CRM Crate

Automatically open & use a lookup control based UI in Dynamics 365?

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.

How to automatically filter and open a lookup field in 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 NameTypeRequiredDescription
allowMultiSelectBooleanNoIndicates whether the lookup allows more than one item to be selected.
defaultEntityTypeStringNoThe default table type to use.
defaultViewIdStringNoThe default view to use.
disableMruBooleanNoDecides whether to display the most recently used(MRU) item.
Available only for Unified Interface.
entityTypesArrayYesThe table types to display.
filtersArray of objectsNoUsed 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.
searchTextStringNoIndicates the default search term for the lookup control. This is supported only on Unified Interface.
showBarcodeScannerBooleanNoIndicates whether the lookup control should show the barcode scanner in mobile clients.
viewIdsArrayNoThe 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.

5 2 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.