Parent child lookup fields in Dynamic CRM

Showing parent-child records in drop-down lists is a common requirement of business applications.

Some examples:

  1. Manager > Employees
  2. Country > State or Province > City
  3. Account category > Account sub-category > Account number

In Dynamics CRM same behaviour can be implemented by using multiple lookup fields with “Related Records Filtering” which is an out of box CRM feature. For this demo consider an entity “Car Finance Application”, which needs some related details like car brand, model and variant.

a

 

 

Steps:

  • Create the following entities and their relationship as described below:
    • Brand
    • Model, it has N:1 relationship with Brand
    • Variant, it has N:1 relationship with Model
  • Add brand, model and variant lookup fields in consumer entity which in this case is CarFinanceApplication:
  • Add brand, model and variant fields in the form
  • Double click on Model lookup field, and do the “Related Records Filtering”  configurations as shown below:  
  • Double click on Variant lookup field, and do the “Related Records Filtering”  configurations as below:

 

  • Save changes + build customizations + test working of lookup field

Form and lookup fields have been configured and selecting brand will show relevant models. As user selects a model, variant look up will list relevant records but there is a catch. If user changes brand or model after selecting variant, values in three lookup fields will not be in sync. Saving record in such scenarios will basically save  incorrect data  and you will agree many times even user will not realise this. An easy approach to fix this issue can be to reset model and variant look up fields when brand field changes and reset variant lookup field when model field changes.

  • From same form window add the following JS as a code library:
function ClearLookUp() {
for (var counter = 0; counter < arguments.length; counter++) {
if (typeof(arguments[counter]) === "string") {
var field = Xrm.Page.data.entity.attributes.get(arguments[counter]);
if (field != null && field.getValue() != null)
field.setValue(null);
}
}
}
  • Double click on “Brand” lookup field and add “ClearLookUp” javascript function on its “onChange” event. Pass execution context and name of lookup field(s) as comma-separated arguments which will need to reset.

 

  • Double click on “Model” lookup field and call “ClearLookUp” function on its “onChange” event. Pass execution context and name of lookup field(s) as a comma-separated argument.
  • That’s it, test your work and go enjoy a coffee if you like 🙂

Considerations:

  • Any number of lookup fields can be configured with this approach
  • Javascript function is a sort of generic, it can be configured with any lookup field without modification. Also just by calling once it can reset any number of lookup fields, the only requirement will be to pass the name of lookup fields as comma-separated arguments.

Credit:

Before this post, I found a similar implementation by Rashan which I have improved.

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s