Show unrelated records in Sub-grid based on Lookup field


This blog intends to showcase the capability of displaying non-related records in a Sub-grid based on a field on the record.

There are 2 steps we need to follow to achieve it.

  1. We change the FetchXml attribute of the sub-grid.
  2. Hide ‘New’ and ‘Open Associated View’ buttons on the grid

1) We change the FetchXml attribute of the sub-grid:

Note: This is unsupported.

Script to change the FetchXML attribute for the sub-grid:
//' Display All Custom for associated Account
function DisplayCustomRecords() {
    //Declare variable
    var AccountID = "";
    var ObjSubGrid = "";

    //'Get SubGrid
    ObjSubGrid = window.parent.document.getElementById("id_of_subgrid");
       if (ObjSubGrid == null) {
        setTimeout(DisplayInitiatedToDoNext, 3000);
    } else {
        //Get Account ID
        AccountID = getLookupDetails('msdyn_serviceaccount');
        if (AccountID!= "") {
            //' Declare fetch XML
            var FetchXML="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"
                                + "<entity name='entityname'>"
                                + "<attribute name='new_id' />"
                                + "<attribute name='new_name' />"
                                + "<attribute name='new_keyaccountid' />"
                                + "<order attribute='new_name' descending='false' />"
                                + "<filter type='and'>"
                                  + "<condition attribute='statecode' operator='eq' value='0' />"
                                  + "</filter>"
                                + "<link-entity name='account' from='accountid' to='new_keyaccountid' alias='aa'>"
                                        + "<filter type='and'>"
                                    + "<condition attribute='accountid' operator='eq' uitype='account' value='" + AccountID + "' />"
                                    + "</filter>"
                                    + "</link-entity>"
                              + "</entity>"
                            + "</fetch>"
            ObjSubGrid.control.SetParameter("fetchXml", FetchXML);
            //Refresh grid to show filtered records only. 
    function getLookupDetails(lookupSchemaName) {
        var lookupObj = Xrm.Page.getAttribute(lookupSchemaName); //Check for Lookup Object
        var lookupRecordGuid;
        //var lookupRecordName;
        if (lookupObj != null) {
            var lookupObjValue = lookupObj.getValue();//Check for Lookup Value
            if (lookupObjValue != null) {
                var lookupEntityType = lookupObjValue[0].entityType, //To get EntityName
                lookupRecordGuid = lookupObjValue[0].id; // To get record GUID
                //lookupRecordName = lookupObjValue[0].name; //To get record
        return lookupRecordGuid;



Note: FetchXMl used in above code is only for representational purpose. You have to generate your own FetchXML.

We will call above function on tab-state change event of the form. And set the tab, not to expand by default.



Note: Also there should be link-entity condition, rather than filter condition for Lookup field, based on which we will populate the sub-grid records.

Note: Another thing I observed is, many people face error, when they have multiple grids in a single tab. It is advised that we have our grid (whose records we are populating using above code), in an individual tab.

2) Hide ‘New’ and ‘Open Associated View’ buttons on the grid:

Another thing that we have to take care of, is to hide the ‘New’ and ‘Open Associated View’ buttons on the grid, since the script that we have written will not work on associated view page.

This we can achieve using Ribbon Workbench. You can attain that by following below link:

There is no need to using the script mentioned in the link. You can directly hide the buttons.