Category: FetchXML

Add custom fetch XML to subgrid lookup

Add custom fetch XML to subgrid lookup

In this blog, I am going to show how we can change subgrid lookup behavior. Basically, I want to control custom N:N contact subgrid lookup, depend on company lookup that is on the contact form.

e.g. A.Datum company have five contacts and if I select contact lookup (custom N:N subgrid inline lookup ) on one of contact form so it’ll only show four contacts in subgrid lookup excluding this contact.

I have an N:N subgrid and company lookup on the contact form.

2

What’s the tricky part for getting subgrid lookup ?

Inspecting DOM

For add event on subgrid lookup, we need to inspect the DOM for subgrid and look out for lookup object.

Have a look below image, It clearly shows how we can find lookup , object by object.(if object key, not the same like below then find lookup with help of object name.)

FYI I am using Dynamics 365 online.

3

After inspecting the DOM structure of the subgrid and carefully understand the mechanics of the lookup filter,we can set filter criteria like in the JavaScript below:

 var RelatedContact = Xrm.Page.getControl('RelatedContact');
 var RelatedContactLookup = RelatedContact.$0_3.$1m_4.$O_4.$38_3.$3_6;
 RelatedContactLookup.addPreSearch(function ()
 {
 if (Xrm.Page.getAttribute("parentcustomerid").getValue())
 {
 var parentCustomer = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].id;
 var strVar = "";
 strVar += "<filter type=\"and\" >";
 strVar +=" <condition attribute=\"statecode\" operator=\"eq\" value=\"0\" />"
 strVar += " <condition attribute=\"parentcustomerid\" 
               operator=\"eq\" value=\"" + parentCustomer + "\" \/>";
 strVar += " <\/filter>";
 strVar += "";
 RelatedContactLookup.addCustomFilter(strVar, "contact");
 }
 })

 

Get the company id from Xrm.Page library  and passes in fetchXML for search criteria.

Our filter criteria code is done but for triggering the above code, we need to attach click function to the subgrid add button, like in JavaScript below :

var RelatedContact_Button = window.parent.document.getElementById("RelatedContact_addImageButton");
 RelatedContact_Button.addEventListener("click", function ()
 {
 ///put your filter code here  
 }

Here is complete code for JavaScript web resource that we need to add on a contact form.

You can also write this code inside a function and call it onload, here I am using self invoking JS function.

(function ()
{
 setTimeout(function ()
 {
 var RelatedContact_Button = window.parent.document.getElementById("RelatedContact_addImageButton");
 RelatedContact_Button.addEventListener("click", function ()
 {
 var RelatedContact = Xrm.Page.getControl('RelatedContact');
 var RelatedContactLookup = RelatedContact.$0_3.$1m_4.$O_4.$38_3.$3_6;
 RelatedContactLookup.addPreSearch(function ()
 {
 if (Xrm.Page.getAttribute("parentcustomerid").getValue())
 {
 var parentCustomer = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].id;
 var strVar = "";
 strVar += "<filter type=\"and\" >";
 strVar += " <condition attribute=\"parentcustomerid\" operator=\"eq\" value=\"" + parentCustomer + "\" \/>";
 strVar += " <\/filter>";
 strVar += "";
 RelatedContactLookup.addCustomFilter(strVar, "contact");
 }
 })
 })
 }, 2000)
}())

You can also download from here.

animation

 

That’s it , Please try and comment below If you found any problem.

Hope it’ll help someone.

 

Note: This is an unsupported way if you really want to go with a supported way then use custom web resource HTML for showing your grid with filter criteria.

Advertisements