We will learn to automatically open & use a lookup control based UI in Dynamics 365. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Dynamics 365.
Why do we need to open a lookup control based custom UI?
Consider a business scenario where we need display a lookup control based UI dialog to an user for selecting the list of records without creating an actual lookup field. We can achieve this requirement with help of the below given solution.
Using JavaScript to automatically open a lookup control UI
We will use the JavaScript and lookupObjects client Web API for opening the lookup control based UI dialog.
Client Web API (lookupObjects ) –
Xrm.Utility.lookupObjects(lookupOptions).then(successCallback, errorCallback)
Opens a lookup control to select one or more items.
The “Object” in this API defines the below given properties.
Property Name | Type | Required | Description |
---|---|---|---|
allowMultiSelect | Boolean | No | Indicates whether the lookup allows more than one item to be selected. |
defaultEntityType | String | No | The default table type to use. |
defaultViewId | String | No | The default view to use. |
disableMru | Boolean | No | Decides whether to display the most recently used(MRU) item. Available only for Unified Interface. |
entityTypes | Array | Yes | The table types to display. |
filters | Array of objects | No | Used to filter the results. Each object in the array contains the following values: filterXml: String. The FetchXML filter element to apply.entityLogicalName: String. The table type to which to apply this filter. |
searchText | String | No | Indicates the default search term for the lookup control. This is supported only on Unified Interface. |
showBarcodeScanner | Boolean | No | Indicates whether the lookup control should show the barcode scanner in mobile clients. |
viewIds | Array | No | The views to be available in the view picker. Only system views are supported. |
JavaScript snippet code –
The below JavaScript function has been triggered during the form’s “OnChange” event.
//CRM Crate - JavaScript Snippet
function CallMethod (executionContext) {
//Reading Attribute Value From The Form.
var formContext = executionContext.getFormContext();
//Reading Value From Input Field
var InputFieldValue = formContext.getAttribute("cc_subscribertext").getValue();
//Define data for lookupOptions
var lookupOptions =
{
defaultEntityType: "cc_subscriber", //Entity schema name
entityTypes: ["cc_subscriber"], //Entity schema name
allowMultiSelect: false, //Select true for multiple selection
defaultViewId:"EA487AD5-3C86-EB11-A812-6045BD726ACE", //Entity view GUID
viewIds:["EA487AD5-3C86-EB11-A812-6045BD726ACE"], //Entity view GUIDs
searchText:InputFieldValue, //Text to be appeared in the lookup search box
filters: [{filterXml: "<filter type='or'><condition attribute='name' operator='eq' value= InputFieldValue/></filter>",entityLogicalName: "cc_subscriber"}] //Filter fetch XML code
};
//Using lookupObjects client API
Xrm.Utility.lookupObjects(lookupOptions).then(
function(success){
//Set the lookupObjects output in entity field
formContext.getAttribute("cc_lookupobjectoutput").setValue("Record ID - " +success[0].id +" | Record Name - " +success[0].name)
console.log("Lookup filtered correctly!");
console.log(success);},
function(error){console.log(error);});
}
Validate the changes in Dynamics 365
Once the above JavaScript snippet has been configured and deploy, navigate to Dynamics 365 and validate the implementation as shown below.
Thus, we learned to automatically filter and open a lookup field in Dynamics 365.