How to show Filtered Lookup Dialog in Dynamics CRM through Script

By | December 19, 2014

We had a requirement where in we were supposed to show a Filtered Custom Lookup on form load. By Filtered Custom Lookup, it meant that we were supposed to create a Lookup dialog similar to CRM Filtered lookup (Filtered Lookup value changes on the basis of other Lookup on which it is dependent) dialog. The lookup has to be filtered on the basis of a value in the lookup field which was there on the form.

Now the question was, how were we supposed to achieve this? We had already done a Lookup Dialog to show all the records for an entity, but this was something new and enthralling.

We dived into it and after a few hours of R&D, we came up with a solution. This blog is entirely dedicated in explaining and implementing that solution.

Let us begin with our entity structure which comprised of an entity called Contractor and it was in N:1 relation with Project and Payment Schedule where as Payment Schedule was in N:1 relation with Project.

The requirement was to show the Filtered Custom Lookup dialog containing Payment Schedule records, related to the Project (Lookup field) selected on the Contractor, on form load of Contractor.

Screenshots:

Note: Lookup Dialog seen in the below screenshots is a Custom Lookup Dialog opened on form load using JavaScript.

Below screenshot shows the Project selected as Test Project having Payment Schedule records for it as a result of which we can see that record in the Lookup Dialog.

Test Payment Schedule

Below screenshot shows the Project selected as Test Project 2 and Test Project 2 has no Payment Schedule records for it and as a result we cannot see any records in the Lookup Dialog.

Payment Schedule Lookup View

Let’s start with the technical part of it.

Code:

//function to open filtered entity lookup to select record

function openFilteredLookup(defaultType, defaultViewId, currentObjectType, currentId, rDependentAttr, rId, rType, relationshipId) {

var functionName = “openLookup”;

try {

 

if (isValid(currentObjectType) && isValid(currentId)) {

//prepare lookup url

var url = “/_controls/lookup/lookupinfo.aspx?AllowFilterOff=0&DefaultType=” + defaultType + “&DefaultViewId=%7b” + defaultViewId + “%7d&DisableQuickFind=0&DisableViewPicker=1&IsInlineMultiLookup=0&LookupStyle=single&ShowNewButton=1&ShowPropButton=1&browse=false&currentObjectType=” + currentObjectType + “&currentid=%7b” + currentId + “%7d&dType=1&mrsh=false&objecttypes=” + defaultType + “&rDependAttr=” + rDependentAttr + “&rId=%7b” + rId + “%7d&rType=” + rType + “&relationshipid=” + relationshipId + “”;

} else {

//prepare lookup url

var url = “/_controls/lookup/lookupinfo.aspx?AllowFilterOff=0&DefaultType=” + defaultType + “&DefaultViewId=%7b” + defaultViewId + “%7d&DisableQuickFind=0&DisableViewPicker=1&IsInlineMultiLookup=0&LookupStyle=single&ShowNewButton=1&ShowPropButton=1&browse=false&dType=1&mrsh=false&objecttypes=” + defaultType + “&rDependAttr=” + rDependentAttr + “&rId=%7b” + rId + “%7d&rType=” + rType + “&relationshipid=” + relationshipId + “”;

}

 

//Set the Dialog Width and Height

var DialogOptions = new Xrm.DialogOptions();

//Set the Width

DialogOptions.width = 500;

//Set the Height

DialogOptions.height = 550;

 

//open dialog

Xrm.Internal.openDialog(Mscrm.CrmUri.create(url).toString(), DialogOptions, null, null, CallbackFunction);

 

//Set the variable to false

isOnLoad = false;

 

} catch (e) {

throwError(e, functionName);

}

}

What are the parameters passed for the above used function?

  • defaultType = Object Type Code of the entity which needs to be shown in the Lookup.
  • defaultViewId = GUID of the defaultView which will be set for the Look In section of the Lookup Dialog, in order to get the defaultView GUID, you can use the following code :

//Get the Default View id

Xrm.Page.getControl(controlName).getDefaultView().replace(“{“, “”).replace(“}”, “”).trim();

    • controlName: It is the name of the Lookup Control.
  • currentObjectType = Object Type Code of the current entity.
  • currentId = GUID of the current record.
  • rDependentAttr = new_contractor.new_projectid
    As the name suggest it`ll give the attribute on the basis of which lookup is filtered.
    The above rDependentAttr value is used in our current demo.

    • new_contractor: It is the name of the current entity.
    • new_projectid: It is the control/attribute name on the basis of which the Lookup is filtered.
  • rId = It is the GUID of the record selected in the new_projectid field.
  • rType = It is the Object Type Code of the new_projectid.
  • relationshipId = new_new_project_new_paymentschedule.

It is the relationship name of how the Project and Payment Schedule entities are related.

Note:

  1. We can retrieve Entity Metadata to get the Object Type Code of any entity.
  2. The above method is an unsupported method to get the Lookup.
  3. How to use Xrm.Internal.openDialog method?

Leave a Reply