How to create Cascading Lookup in Microsoft Dynamics CRM
Assume there are three entities, as it is shown on Screen 1. The relations between those entities are displayed on Screens 2 and 3. Let’s create a cascading (master detail) lookup.
Screen 1
Screen 2
Screen 3
Screen 4
City lookup should be configured to show only the cities that are relevant to the selected Country lookup value in the Addresses entity. Open the city field properties dialogue in the form (See Screen 5).
To do that:
- Open the desired solution.
- Select Addresses entity Forms
- Open default form
- Select City Lookup
- Click on the Change Properties button
Screen 5
As a result, the window displayed on Screen 6 appears. Dialogue menu options can be modified to display only the relevant data (See Screen 6).
Screen 6
Now “City” displays the relevant data only. Still, there is a possibility to select a city and then a country not relevant to it. This may lead to non-synchronized data (Addresses Country and Cities Country). In this case, the incorrect data will be displayed to a user. But if the field is used in the business logic, it may cause even more troubles. To resolve this issue, validation of input data can be implemented on the SERVER side.
How to solve synchronization issue with GUI
Add JavaScript to disable and reset City Lookup when the user changes Country.
//CascadingLookUp.js
function CascadingLookUpSample_Init() {
Xrm.Page.getControl(“ut_city”).setDisabled(crmForm.all.ut_country.DataValue == null);
}
function CascadingLookUpSample_CantryOnChange() {
crmForm.all.ut_city.DataValue = null;
Xrm.Page.getControl(“ut_city”).setDisabled(crmForm.all.ut_country.DataValue == null);
}
Now bind events to the abovementioned functions. (See Screen 7, 8)
To bind CascadingLookUpSample_Init() on the form load event:
- Open the form
- Click on the Form Properties ribbon button
- Select Events tab in the Form Properties dialogue
- Add library to the libraries section, if it wasn’t done earlier
- Select OnLoad event from the dropdown and click the Add button placed in the Event Handlers section
- Set up library and handler function name in Handler Properties dialogue
Screen 7
To bind CascadingLookUpSample_CantryOnChange() to Country Lookup’s on change event:
- Select Country Lookup in the form
- Click on the Change property ribbon button
- Select Event tab in Field Properties dialogue
- Set up Library to CascadingLookUp and Function to CascadingLookUpSample_CantryOnChange
Screen 8
How to Add Custom View