Tuesday, November 19, 2019

Paging in Dynamic 365 CRM using JavaScript


The paging works differently depending on whether you’re performing a straight Rest API query or using FetchXML in the request.

Paging Rest API Queries example.
Item count per page is specified using odata.maxpagesize=x when setting Request Header.
It is ideally used when showing result in a GRID and using pagination to display next set of records.
If odata.maxpagesize is not mentioned Dynamic CRM default maxpagesize is 5000 records.
odata.maxpagesize Does NOT work for Fetch XML request

httpRequest.setRequestHeader("Prefer", "odata.maxpagesize=10");

To fetch next set of record we need to open a new request specified in @odata.nextLink attribute
@odata.nextLink does not applies when request is of Fetch Xml

JSON.parse(httpRequest.responseText)["@odata.nextLink"]

CODE EXAMPLE for Select Clause

var serverUrl = Xrm.Page.context.getClientUrl(); 

// sample function to return all the leads
function GetAllLeadsViaSelectQuery()
{
  var selectLeadQuery = "$select=fullname,leadid";
  var leadsQueryUrl = serverUrl + "/api/data/v9.1/leads?" + selectLeadQuery;

  // call our new method
  var retrievedLeads = getallRecords(leadsQueryUrl);

  alert(retrievedLeads.results.length);

  alert(retrievedLeads.results[0].fullname);   
}


function getallRecords(OriginalQueryUrl)
{
// we return an object with a similar structure
var allRecords = new Object();
allRecords.results = new Array();

// we loop until we have an url to query
var queryUrl = OriginalQueryUrl;
while (queryUrl != null)
{
    // we build the request
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", queryUrl, false); // false = synchronous request
    httpRequest.setRequestHeader("Accept", "application/json");
    httpRequest.setRequestHeader("OData-MaxVersion", "4.0");
    httpRequest.setRequestHeader("OData-Version", "4.0");
    httpRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    httpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

   httpRequest.setRequestHeader("Prefer", "odata.maxpagesize=500");

    httpRequest.send();

    if (httpRequest.status === 200) {
        var parsedResults = JSON.parse(httpRequest.responseText);

        if (parsedResults != null && parsedResults.value != null)
        {
            // we add the results to our object
            for (var i = 0; i < parsedResults.value.length; i++)
            {
                allRecords.results.push(parsedResults.value[i]);
            }

            //JSON.parse(httpRequest.responseText)["@odata.nextLink"]

            // check if there are more records and set the new url, otherwise we set to null the url
            if (parsedResults["@odata.nextLink"] != null && parsedResults["@odata.nextLink"] != 'undefined') {
                queryUrl = parsedResults["@odata.nextLink"];
            }
            else {
                queryUrl = null;
            }
        }
    }
    else
    {
        // if the request has errors we stop and return a null result
        queryUrl = null;
        allRecords = null;
    }
}
return allRecords;
}

2 comments:

BK said...

Sir you are an amazing human being. Thank you!

BK said...

Sir you are a lifesaver and a wonderful human being.