Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

How to set up custom fetchXML for Subgrid in Dynamics CRM

Posted on by 3,610

I have following fetchXML that I need to set up for a subgrid on Invoice form. I s there any idea how it can be achieved in supported way?

var orderId = Xrm.Page.getAttribute("salesorderid").getValue()[0].id;
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                    "  <entity name='systemuser'>" +
                    "    <attribute name='fullname' />" +
                    "    <attribute name='businessunitid' />" +
                    "    <attribute name='title' />" +
                    "    <attribute name='address1_telephone1' />" +
                    "    <attribute name='positionid' />" +
                    "    <attribute name='systemuserid' />" +
                    "    <order attribute='fullname' descending='false' />" +
                    "    <link-entity name='new_salesorder_systemuser' from='systemuserid' to='systemuserid' visible='false' intersect='true'>" +
                    "      <link-entity name='salesorder' from='salesorderid' to='salesorderid' alias='ab'>" +
                    "        <filter type='and'>" +
                    "          <condition attribute='salesorder' operator='eq' value='" + orderId + "' />" +
                    "        </filter>" +
                    "      </link-entity>" +
                    "    </link-entity>" +
                    "  </entity>" +
                    "</fetch>";



  • RHOU Profile Picture
    RHOU 65 on at
    RE: How to set up custom fetchXML for Subgrid in Dynamics CRM

    I just created a very detailed feature wish to make this happen in a supported way for Unified Interface here:

    PLEASE VOTE!

    powerusers.microsoft.com/.../675538

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to set up custom fetchXML for Subgrid in Dynamics CRM

    Please follow the Link, I have done this in my project and it is working, do like the link page and mark the answer correct.

    https://sank8sinha.wordpress.com/2020/01/07/adding-filtered-views-in-uci-in-dynamics-365-crm-finally-achieved/

  • Suggested answer
    Silent Serenity Profile Picture
    Silent Serenity 55 on at
    RE: How to set up custom fetchXML for Subgrid in Dynamics CRM

    Hi Mohsin,

    For D365 CRM V.9 I used this code below, Assuming you already set up the subgrid in the form as "All related records":

    function FetchViaName(executionContext) {

       var formContext = executionContext.getFormContext();

       // Credit_Performance : is name of subgrid given on Form.

       // You need the code below because using SetParameter in formContext.getControl("Credit_Performance") will not work.

       var objSubGrid = window.parent.document.getElementById("Credit_Performance");

       var conSubGrid = formContext.getControl("Credit_Performance");

       var subRowCount = conSubGrid.getGrid().getTotalRecordCount(); // Making sure that you the grid is loaded

       var formContext = executionContext.getFormContext();

       var globalContext = Xrm.Utility.getGlobalContext();

       var version = globalContext.getVersion();

       // You need the timeout for at least the form is properly loaded.

       if (subRowCount == 0) {

           setTimeout(FetchViaName(executionContext), 5000);

       } else {

           // When subgrid is loaded, get the other relevant value.

           var curId = formContext.data.entity.getId();

           var curName = formContext.getAttribute("dxcts_name").getValue();

           var ratingName = formContext.getAttribute("dxcts_searchratingname").getValue();

           var workAccount = formContext.getAttribute("dxcts_workaccount").getValue();

           //Create FetchXML for sub grid to filter records

           var FetchXml = '<fetch version="1.0" output-format="xml - platform" mapping="logical" distinct="false"> '

               + '<entity name = "dxcts_creditperformance" > '

               + '<attribute name="dxcts_creditperformanceid" /> '

               + '<attribute name="dxcts_name" /> '

               + '<attribute name="dxcts_work" /> '

               + '<attribute name="dxcts_ratings" /> '

               + '<attribute name="dxcts_comments" /> '

               + '<attribute name="dxcts_sugestions" /> '

               + '   <attribute name="createdon" /> '

               + '   <order attribute="dxcts_name" descending="false" /> '

               + '   <filter type="and"> '

               + '       <condition attribute="dxcts_work" operator="eq" uiname="' + curName + '" uitype="dxcts_work" value="' + curId + '" /> '

               + '       <condition attribute="dxcts_accountscreditperformanceid" operator="eq" uiname="' + workAccount[0].name + '" uitype="account" value="' + workAccount[0].id + '" /> '

               + '       <condition attribute="statuscode" operator="eq" value="1" /> '

               + '       <condition attribute="dxcts_name" operator="like" value="%' + ratingName + '%" />'

               + '   </filter> '

               + ' </entity > '

               + ' </fetch > ';

           // Layout of subgrid. It's up to you if your going to use this code, it's just creating/identifying a new layout.

           //var LayoutXml = "<grid name='resultset' object='8' jump='new_name' select='1' preview='1' icon='1'>" +

           //    " <row name='result' id='new_boat'>" +

           //    "<cell name='dxcts_name' width='100' />" +

           //    "<cell name='dxcts_work' width='200' />" +

           //    "<cell name='dxcts_ratings' width='100' />" +

           //    "<cell name='dxcts_comments' width='200' />" +

           //    "<cell name='dxcts_sugestions' width='200' />" +

           //    "</row>" +

           //    "</grid>";

           // Apply layout and filtered fetchXML

           //objSubGrid.control.SetParameter("layoutXml", LayoutXml);

           objSubGrid.control.SetParameter("fetchXml", FetchXml);

           //Refresh grid to show filtered records only.

           objSubGrid.control.Refresh();

       }

    }

    Happy Coding! =D

  • Verified answer
    Mohsin Ali Profile Picture
    Mohsin Ali 3,610 on at
    RE: How to set up custom fetchXML for Subgrid in Dynamics CRM

    I achieve this using Quick View form. Everything else throw the exception, so QVF worked for me.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to set up custom fetchXML for Subgrid in Dynamics CRM

    Hi,

    Try this too..

    social.microsoft.com/.../trying-to-set-subgrid-fetchxml-with-subgrid1controlsetparameterfetchxml-fetchxml

    Thanks..

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
  • Mohsin Ali Profile Picture
    Mohsin Ali 3,610 on at
    RE: How to set up custom fetchXML for Subgrid in Dynamics CRM

    'SetParameter' is undefined.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to set up custom fetchXML for Subgrid in Dynamics CRM

    Hi Mohsin,

    Follow this link:-

    blogs.msdn.microsoft.com/.../filtering-in-sub-grids-crm-2015-crm-online

    https://community.dynamics.com/crm/f/117/t/164732

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,513 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,862 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans