Microsoft Dynamics CRM 2015 – Filtered Lookup for “Add Existing…” Button of a CRM N:N View – SubGrid

There was a situation where it was required for a sub-grid with a view in relation N:N with the current entity that was edited. This can be easy achived by adding a N:N relationship and then adding a view and select “related only” for the sub-grid that is added to the form that is being edited.

So far so good. Then it was required for the grid to be filtered depending on the value of an option set filed. When the end user is changing the value of the filed then the new filter needs to be applied to the sub-grid.

 

This can be achived with the following code:


var fetchXml = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''].join(" ");

var gridHtml = document.getElementById('mysubgrid');
gridHtml.control.SetParameter("fetchXml", fetchXml);

gridHtml.control.refresh();

The grid data is displayed correctly in the grid and it can be observed that the lookup in the sub-grid does not filter the data in the same manner as is it filtered in the grid. After all if the data is filtered in the grid it should be filtered also in the lookup.

After inspecting the DOM element of the sub-grid the solution was found to add a call for addPreSearch handler like in the following example:

document.getElementById("mysubgrid_addImageButtonImage").addEventListener("click", function () {
                setTimeout(function () {
                    var gridControl = Xrm.Page.getControl("mysubgrid");
                    var me = gridControl.$c_0.$N_4.$Y_3;
                    me.addPreSearch(function () {
                        me.addCustomFilter('');
                    });
                }, 2000);
            });

Update: CRM 2013
According to the article title, the above script works for CRM 2015. However, if you need to have the same functionality for CRM 2013 in the same manner as for CRM 2015 the script needs to be updated with “gridControl.$c_0.$I_4.$3M_3.$3_6” like in the following code:

document.getElementById("mysubgrid_addImageButtonImage").addEventListener("click", function () {
                setTimeout(function () {
                    var gridControl = Xrm.Page.getControl("mysubgrid");
                    var me = gridControl.$c_0.$I_4.$3M_3.$3_6;
                    me.addPreSearch(function () {
                        me.addCustomFilter('');
                    });
                }, 2000);
            });

5 comments

  1. Hi George!

    I have a CRM 2015 Online Sp1 and i donĀ“t know how the script need to be updated with the instruction about gridcontrol. In this CRM shows a message that $N_4 is an invalid parameter. The grid control has a $1_3 parameter only.

    Can you help me?
    Thanks in advanced
    Carol Solorzano

    1. Hello Carol,

      Try increasing the timeout until you get to consume your grid object. The structures described in the post work properly on both CRM 2015 & 2015 (on-premise and online) as long as the grid control is fully rendered.

      Thank you,
      Cornel

    1. Hello Ankita,

      “mysubgrid_addImageButtonImage” is the id of the “+” or “Add” button (top-right corner of the subgrid). You simply replace “mysubgrid” with the id of your subgrid and you can access that grid’s “+” button in order to attach custom events to it.

      Cornel

Leave a Reply to Cornel Croitoriu Cancel reply

Your email address will not be published. Required fields are marked *