web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics 365: Unified Interface: Inject FetchXml into Subgrid

(0) ShareShare
ReportReport
Posted on by

Hello,

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.

If anyone did research how to do it, please advise.

Thanks in advance.

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Im facing similar issue, if you've found a solution please let us know.

    regards

  • Verified answer
    Community Member Profile Picture
    on at

    ELCHE38,

    Yes, I have found a solution. Maybe this is not a good solution but it works for us.

    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();
            }
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    As you already mentioned this is not supported , so is there any specific requirement you are still using this ?  I would suggest not to use unsupported/undocumented code ,  instead of this you may implement following options -

    - Create only exact records in the entity  which you want to show in the sugrid  or  add additional fields to add the condition you want to select data with helps of  OOB sub grid filter criteria. For example you want to display certain data which having some condition like Type = premium( This is dynamic value )

     So you can create another fields in the subgrid entity  with set null or premium so that you can easily filter with contains data operator.

    - You can also create separate entity  where you can only insert filtered data which you want to show and add the entity as a subgrid. You can prepare the data in such a way so that you can implement OOB filter criteria .

     - Another options would be create Retrieve Multiple plugin and you just need to add the filter criteria, You can refer below  article  , this is great idea , I am also using same , it will change the filter in servervside.

           hachecrm2011.wordpress.com/.../filtering-views-by-intercepting-retrieve-multiple-queries-with-a-plugin

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hi Goutam,

    Thank you for your ideas. Yes, we have a requirement to update the sub-grid on the salesorder entity every time when a lookup field is changed and populate this subgrid with other orders records that are related to this lookup field.

    I think your the third option can help in this case. But it could be a performance issue because we have a lot of Retrieve Multiple operation for the salesorder entity.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Andemki,

    This is not good idea to use OOTB subgrid here , how do you know the lookup value which has selected for the record from retrieve multiple plugin?

    I would suggest to use custom HTML webresource where you build your custom html grid by getting data using web API retrieve operation. You can also use third party custom grid whatever you preferred.

  • Community Member Profile Picture
    on at

    Goutam,

    Thank you for your idea with a custom HTML webresource. But we will try to use unsupported way at first to keep native control for the users.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Andemki,

    You can also follow below steps , this is completely supported way and  no code approach.

    -- Create a quick view forms in the lookup entity .

    -- Add the subgrid in the quick view forms .

    - - Include the quick view forms where you want to display subgrid.

    -- Now change the lookup value , it will render related data in the subgrid.

    Hope this helps n.

  • chauhanhardik Profile Picture
    687 on at

    Hi Andemki,

    can you please elaborate some more about the condition in below snippet:

    var newFunc = setFetchXmlStr.replace("function(e){", "function(e){if (e.indexOf('ZZZAAA') >= 0) {e = fetchXml;}");

    Also if you can explain about "grid.addOnLoad(onLoadFunction);"

    Thanks in advance.

    Hardik Chauhan

  • Community Member Profile Picture
    on at

    Hi Hardik,

    Yes, sure.

    if (e.indexOf('ZZZAAA') >= 0) {e = fetchXml;}

    "Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml" function accepts a parameter "e", which is a fetch xml. The internal CRM logic can call this function several times and pass fetch xml values to this function. We need to check if it is a fetch xml that you want to override from your specific sub-grid. To make it easy we can go to the sub-grid's view and create a new condition (e.g. "someattribute" contains "ZZZAAA" (or whatever)). And if "e" parameter contains our "ZZZAAA", it means that we got our initial fetch xml from the sub-grid's view. Then we need to copy our custom fetch xml (fetchXml) into "e" variable to get data from our custom fetch xml that we build dynamically.

    grid.addOnLoad(onLoadFunction)

     - is not required in this case. It allows to call your custom function every time when your sub-grid is refreshed. We use it to show notifications based on sub-grid's data.

  • chauhanhardik Profile Picture
    687 on at

    Hi Dmitry,

    I'm trying to override sub-grid's data with below fetchXml data:

    var currentAccount = Xrm.Page.data.entity.getId();

    <fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0" >

       <entity name="contact" >

           <attribute name="contactid" />

           <filter type="and" >

               <condition attribute="statecode" operator="eq" value="0" />

               <filter type="or" >

                   <condition attribute="parentcustomerid" operator="eq" value="" + currentAccount + "" />

               </filter>

           </filter>

       </entity>

    </fetch>

    Requesting you to help me over this.

    Thanks,

    Hardik Chauhan

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans