Filter N:N Sub-Grid in Dynamics 365 without Custom Actions


Below code is for only N:N Relationship Sub-Grids. Copy the below code and register on OnLoad event of the Parent Entity Form on which the Sub-Grid exists.

Replace the <Sub-Grid Name>, <Form Lookup Field Name>,ย new_testingid, <Schema name of the Related Entity>.

//On Load of the Form

function onLoad() {

setTimeout(function () {

//get the element based on the <Sub-Grid Name>_addImageButton

var subGrid_ImageButton = window.parent.document.getElementById(“<Sub-Grid Name>_addImageButton”);

subGrid_ImageButton.addEventListener(“click”, function () {

//Get the Sub-Grid Control

var relatedEntity_SubGrid = Xrm.Page.getControl(‘<Sub-Grid Name>’);

//Get the Sub-Grid Lookup

var relatedEntity_SubGrid_Lookup = relatedEntity_SubGrid.$0_3.$1m_4.$O_4.$38_3.$3_6;

//Add the Pre-Search for Sub-Grid Lookup

relatedEntity_SubGrid_Lookup.addPreSearch(function () {

//Check whether the Lookup field on Form has value

if (Xrm.Page.getAttribute(“<Form Lookup Field Name>”).getValue()) {

//Get the ID of Lookup field on Form

var lookupIdOfLookupField = Xrm.Page.getAttribute(“<Form Lookup Field Name>”).getValue()[0].id;

//Build the Fetch Condition

var fetchCondition = “”;

fetchCondition += “<filter type=\”and\” >”;

fetchCondition += ” <condition attribute=\”new_testingid\” operator=\”eq\” value=\”” + lookupIdOfLookupField + “\” \/>”;

fetchCondition += ” <\/filter>”;

fetchCondition += “”;

relatedEntity_SubGrid_Lookup.addCustomFilter(fetchCondition, “<Schema name of the Related Entity>”);

}

})

})

}, 2000)

}

You are all set. ๐Ÿ™‚

 

Cheers!!! ๐Ÿ™‚

 

 

Advertisements

About Kishore Dhanekula

MS Dynamics CRM and .NET Architect
This entry was posted in Dynamics 365, HTML, JavaScript, MS Dynamics 365, MS Dynamics CRM 2016 and tagged , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Filter N:N Sub-Grid in Dynamics 365 without Custom Actions

  1. Tanis says:

    I could not get this to work in onpremise CRM 2013, but the modifications shown below (based on your example merged with https://missdynamicscrm.blogspot.com.au/2016/05/filter-n-to-n-many-to-many-subgrid-lookup-existing-record-form-script.html) allowed me to accomplish it:

    function preparePreSearch() {
    var myViewGuid = “{D4EDF1E1-85CD-E711-811C-005056B4252F}”;
    var myViewEntity = “my_entityname”;
    var myViewName = “My view name”;

    setTimeout(function() {
    var subGrid_ImageButton = document.getElementById(“SUBGRIDNAME_addImageButton”);
    subGrid_ImageButton.addEventListener(“click”,function() {
    setTimeout(function() {
    var inlineElement = document.getElementById(“lookup_SUBGRIDNAME_i”);
    var inlineBehaviour = inlineElement.InlinePresenceLookupUIBehavior;
    inlineBehaviour.AddCustomView(myViewGuid,myViewEntity,myViewName,getFetchXml(),getLayoutXml(), true);
    }, 500);
    });
    }, 1000);
    }

  2. andriibutenko says:

    Is it supported? ๐Ÿ™‚

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s