Dynamics 365: Unified Interface: Inject FetchXml into Subgrid

982 views
Skip to first unread message

vijay kumar

unread,
May 8, 2019, 2:42:07 AM5/8/19
to Microsoft Dynamics CRM
Hi,

We have a sub-grid on the entity's form and the data of this sub-grid depends on some field's value.

To accomplish this we use

grid.control.SetParameter('fetchXml', query);

where grid is an HTML element of the sub-grid on the entity's form query is a string which contains a fetch xml query with our field's value.

I know that it is an unsupported way and it works for Dynamics 365 CRM with classic UI but it doesn't work in the v.9 unified interface.

I have spent some time debugging internal CRM scripts and found out that there is a Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery object. And I can call it's "set_FetchXml" function to inject my own fetch xml query into this grid. And it works. But I didn't find a way how to get this object from a sub-grid HTML element.

Vikranth Sinha

unread,
May 17, 2019, 6:15:01 AM5/17/19
to Microsoft Dynamics CRM
Hi,

The main idea is to override the "Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml" function to inject your fetch xml that you create dynamically for the sub-grid.

It is not a good idea to override the whole function and it is enough to insert two code lines at the beginning of the function by using "eval" function:

    UpdateSetFetchXmlFunc: function (fetchXml) {
        var setFetchXmlStr = Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml.toString();
        var newFunc = setFetchXmlStr.replace("function(e){", "function(e){if (e.indexOf('ZZZAAA') >= 0) {e = fetchXml;}");
        eval("Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml=" + newFunc);
    }

where

fetchXml parameter is your fetch xml that you want to use in your sub-grid.
'ZZZAAA' is a specific text to recognize initial fetch xml in your view. For example, "<condition attribute="field_name" operator="like" value="ZZZAAA%" />"

And you can use this function like this:

        var grid = Xrm.Page.ui.controls.getByName("sub-grid name");
        if (grid) {
            grid.addOnLoad(onLoadFunction);
            var query = "your query"
            UpdateSetFetchXmlFunc(query);
            grid.refresh();
        }

If you still need help regarding Microsoft Dynamics CRM Development feel free to contact me.
Reply all
Reply to author
Forward
0 new messages