0

I've following code snippet to set up the fetchxml of a subgrid on invoice form but it throws the exception: Cannot read property 'SetParameter' of undefined.

Any Idea how it could be resolved.

Code Snippet:

function filterUsers() {
    try {
        debugger;
        var orderId = Xrm.Page.getAttribute("salesorderid").getValue()[0].id;
        var spGrid = getSubgrid("new_salesperson");
        //var spGrid = Xrm.Page.getControl("new_salesperson");
        if (spGrid == null || spGrid == 'undefined') {
            setTimeout(filterUsers, 500);
            return;
        }
        else {
            var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                    "  <entity name='systemuser'>" +
                    "    <attribute name='fullname' />" +
                    "    <attribute name='title' />" +
                    "    <attribute name='address1_telephone1' />" +
                    "    <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>";
            spGrid.control.SetParameter("fetchXml", fetchXml);
            spGrid.control.SetParameter("effectiveFetchXml", fetchXml);
            spGrid.control.SetParameter("fetchXmlForFilters", fetchXml);
            spGrid.control.refresh();
        }
    }
    catch (e) {
        if (e.message == "Cannot read property '0' of null" || e.message == "orders[0] is undefined" || e.message == "Unable to get property '0' of undefined or null reference")
            return;
        alert("Error: " + e.message);
    }

}

function getSubgrid(elementName) {
    debugger;
    if (document.getElementById(elementName) == null || document.getElementById(elementName) == 'undefined') {
        return window.parent.document.getElementById(elementName);
    }
    else {
        return document.getElementById(elementName);
    }
}
Mohsin Tester
  • 35
  • 1
  • 8

1 Answers1

3

Manipulating the DOM directly in CRM is not supported. Find another way.

It appears to me that you are trying to add a subgrid of users to the invoice form. The users have been associated with sales orders with your custom relation, new_salesorder_systemuser.

I would suggest simply creating a quick view form on sales order with the subgrid of users and selecting Only Related Records. You can then add this quick view form to your invoice form.

Henrik H
  • 5,747
  • 1
  • 21
  • 33
  • I've that subgrid already. But I need to have same subgrid on invoice form as well. – Mohsin Tester Nov 09 '16 at 13:23
  • Basically, that subgrid is acting like list of salespeople, so I need same list of users on invoice form as well acting like salesperson for that specific order selected in "Order Number" look up field – Mohsin Tester Nov 09 '16 at 13:24
  • Which subgrid do you have already? The one I suggest putting on a quick view form on sales order, and then adding the quick view form to your invoice form? – Henrik H Nov 09 '16 at 13:27
  • I don't have quick view form placed over order form but it is sub-grid of users – Mohsin Tester Nov 09 '16 at 13:30
  • In summary: Don't manipulate the DOM. If I understand your scenario correctly, my suggestion should work. Feel free to re-read the answer and give it a try. – Henrik H Nov 09 '16 at 13:31