0

I've subgrid on Account form, I am trying to get list of Orders of child contact and then add to subgrid. So, far I did the following scripts but it throws error that cannot read property 'SetParameter' of undefined. Can anyone please guide how can I change fetchxml of a subgrid?

function listOfOrders(contactId){
    var orderFetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
                    "  <entity name='salesorder'>"+
                    "    <attribute name='name' />"+
                    "    <attribute name='customerid' />"+
                    "    <attribute name='salesorderid' />"+
                    "    <attribute name='statecode' />"+
                    "    <attribute name='createdon' />"+
                    "    <order attribute='name' descending='false' />"+
                    "    <filter type='and'>"+
                    "      <condition attribute='customerid' operator='eq' uitype='contact' value='"+contactId+"' />"+
                    "    </filter>"+
                    "  </entity>"+
                    "</fetch>";

    var retrievedOrders = XrmServiceToolkit.Soap.Fetch(orderFetchXml);

        var contactId = "";
        if(retrievedOrders.length <0)
            return;

    var viewId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
    var viewDisName = "Account Orders subgridview";

     var layOutXml = "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
                            "<row name='result' id='salesorderid'>" +
                            "<cell name='name' width='300' />" +
                            "<cell name='statecode' />"+
                            "<cell name='createdon' />"+
                            "<cell name='customerid' />"+   
                            "disableSorting='1' />" +
                            "</row>" +
                    "</grid>";
    var orderSubgrid = window.parent.document.getElementById("orderssubgrid");
    //apply layout and filtered fetchXML
    orderSubgrid.control.SetParameter("layoutXml", layOutXml);
    orderSubgrid.control.SetParameter("fetchXml", orderFetchXml);
    //Refresh grid to show filtered records only. 
    orderSubgrid.control.Refresh();
}
M. Ali
  • 37
  • 6

2 Answers2

1

There is a supported no-code way to do this.

  • Create a QuickView in Contact entity containing the subgrid to Orders
  • Add the QuickView to the Account entity linking it to the Contact lookup

If the Contact is not from a lookup, you can

  • Add a hidden lookup on Contact in the Account form
  • Make your javascript fill this lookup with the reference to the contact you want to filter the subgrid on (remember to .fireOnChange() after filling the field, to invoke the next step)
  • invoke refresh() to update the subgrid in the onchange of the contact lookup you just filled

Then implement the quickview like the two points above.

Alex
  • 23,004
  • 4
  • 39
  • 73
0

Yes you could use a timeout. The code would look like:

function listOfOrders(contactId) {
    var orderSubgrid = window.parent.document.getElementById("orderssubgrid");
    if (orderSubgrid == null) {
        setTimeout(function () { listofOrders(contactId); }, 2000);
        return;
    }
    // ... the rest of your code
}

If you wanted to try a supported method there were new subgrid methods added to Xrm in 2016. You might want to see if it's possible to use setCurrentView.

Dave Clark
  • 2,243
  • 15
  • 32
  • I called debugger with the snippet that you provided it seems that subgrid loaded before reaching to SetParameter. **orderSubgrid = div#orderssubgrid.ms-crm-ListControl-Ex-Lite** is what I've in debugger – M. Ali Mar 31 '17 at 10:05
  • Then there should be no issue? If the subgrid is loaded before reaching `orderSubgrid.control.SetParameter()` then you shouldn't receive the error **cannot read property 'SetParameter' of undefined**. If there is an issue, you could instead check that you have the `control` property defined too: `if (orderSubgrid.control != null)` – Dave Clark Mar 31 '17 at 10:31
  • That is what I don't found in debugger – M. Ali Mar 31 '17 at 10:44