Search code examples
dynamics-crm-2011dynamics-crmmicrosoft-dynamicsdynamics-crm-2016

getting list of records in a subgrid using webapi or fetchxml


I want to get list of all records that are in a subgrid in a form

Is there a way to get it using some api call or using javascript? The below code will fetch the rows if the subgrid is available in the form but I don't want to have the subgrid in the form where I am going to fetch these records

var selectedRows = Xrm.Page.getControl("Contacts").getGrid().getSelectedRows();
var rows = Xrm.Page.getControl("Contacts").getGrid().getRows();

For example, assume that I have a medicalCase entity form, where I am having patients subgrid. I want to get the list of records in the patients subgrid using webapi in some other entity form


Solution

  • Yes, you can retrieve records using fetch.

    1. Determine the fetchxml behind your view. Easiest way to do this is to pull up your view in Advanced Find and then use the Download Fetch Xml button.
    2. Use a library like the xrmservicetoolkit to execute your fetch statement.

      var fetchXml =
              "<fetch mapping='logical'>" +
                 "<entity name='contact'>" +
                    "<attribute name='contactid' />" +
                    "<attribute name='firstname' />" +
                    "<attribute name='lastname' />" +
                    "<attribute name='accountrolecode' />" +
                    "<filter>" +
                       "<condition attribute='contactid' operator='eq' value='" + contactId + "' />" +
                    "</filter>" +
                 "</entity>" +
              "</fetch>";
      
      var retrievedContacts = XrmServiceToolkit.Soap.Fetch(fetchXml);
      console.log(retrievedContacts[0].attributes['lastname'].value)
      

      See this page for more info.