
In my previous article I outlined how to invoke a lookup dialog on a Freedom UI page. In this article, we’ll expand on that idea to allow adding to a list from a lookup. For this sample, we have a list on the account page of products owned by the account. We’ll add a button to invoke a lookup to allow the user to select one or multiple products to add to the account’s product list.
The end result will be as follows:
The code to accomplish this will be very similar to the previous article. The main difference will be that instead of flattening out the text values of the selected items, we’ll use a model Insert to add the records.
View Inserting a Record from Client-Side Code Using the Model Class
Note, first make sure you’re adding the Creatio devkit to the page as sdk (see article above). The code for the request of the click of our button will look like this:
request.$context.executeRequest({ type: "crt.OpenLookupPageRequest", $context: request.$context, entitySchemaName: "Product", caption: "Select products to add to the account", features: { select: { multiple: true, selectAll: false, resultType: 'lookupValues' }, create: { enabled: false } }, afterClosed: async function(selectedItems) { // add each selected item from the lookup to the list const accountId = await request.$context.Id; const accountProdModel = await sdk.Model.create("UsrAccountProduct"); selectedItems.forEach(product => { accountProdModel.insert({ UsrAccount: accountId, UsrProduct: product.value }); }); } });
To make it so the items appear in the list without the need to refresh it, simply enable live data update for the entity you’re adding to.
Information on how to add this custom code to the action of when a button is clicked would be useful. I think there is another CustomerFX article on it that could be used to link to for this purpose.
Yes. The article that shows how to add custom code for the click of a button can be found here: https://customerfx.com/article/adding-a-button-to-execute-custom-code-on-a-creatio-freedom-ui-page/
Ryan