CRM Crate

Automatically change the lookup field’s target entity in Dynamics 365 CRM

In this article, we will learn to automatically change the lookup field’s target entity in Dynamics 365 CRM.

Automatically change the lookup field's target entity in Dynamics 365 CRM

Why do we need to dynamically change the lookup’s target entity?

Consider a scenario where we are using a Customer Lookup field which have two entities namely Account and Contact. Now, we can easily filter the customer field to display the record for only one entity (Either Account or Contact) as per the business requirement with help of the below given solution.

Automatically changing the lookup’s target entity –

We will use the setEntityTypes client Web API to dynamically change the attribute’s target entity as shown below.

formContext.getControl(arg).setEntityTypes([entityLogicalNames]);

Sets the types of tables allowed in the lookup control.

JavaScript Code Snippet –

For the demonstration purpose, we will dynamically change the entity of a customer lookup attribute as per the inputs received from an entity form. Therefore, we have deployed the below given JavaScript snippet during the “OnChange” event of field “Lookup Entity Type”.

//CRM Crate - JavaScript Snippet
function CallMethod (executionContext) {
//Reading Attribute Value From The Form.
var formContext = executionContext.getFormContext();

//Reading Value From Input Field
var Input = formContext.getAttribute("cc_lookupentitytype").getValue();
if(Input == 1) // Account Value (Optionset)
{

//Using Client API To Set Target Entity
formContext.getControl("cc_customerlookup").setEntityTypes(["account"]);
}
else if(Input == 2) // Contact Value (Optionset)
{

//Using Client API To Set Target Entity
formContext.getControl("cc_customerlookup").setEntityTypes(["contact"]);
}
}

Validating the changes in Dynamics 365 –

Once the above JavaScript snippet is deployed and configured, validate the implementation in the Dynamics 365 CRM as shown below.

  • The customer lookup attribute’s target entity is automatically set to “Account” when an option-set field value is updated to “Account”.
  • The customer lookup field’s target entity is automatically set to “Contact” when an option-set field value is updated to “Contact”.

Thus, we learned to automatically change the lookup field’s target entity in Dynamics 365 CRM.

5 1 vote
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.