Paging in D365 Customer Engagement v9.0 - CloudFronts

Paging in D365 Customer Engagement v9.0

Posted On July 10, 2018 by Clinton D'Mello Posted in  Tagged in

Introduction:

The Xrm.retrieveMultipleRecords method is used to retrieve a collection of records in Dynamics 365 Customer Engagement . In this blog we will demonstrate how we can use paging and fetch more than 5000+ records.

In CRM when we fetch records using code we only get the first 5000 records, in some cases there are more than 5k records that need to be fetched, we can achieve this using paging.

In this blog for the demonstration purpose, we will fetch 3 records per page so that we can see how the paging functionality works in D365 v9.0

Implementation:

Step 1: The syntax is as shown below:

Xrm.WebApi.retrieveMultipleRecords(entityLogicalName, options, maxPageSize).then(successCallback, errorCallback);

Here the in options parameter we specify the query. In our example we will be fetching all the accounts in the system.

In the maxPageSize parameter we specify the number of records to be returned per page. By default the value is 5000. In this example we set the maxPageSize as 3 which will return 3 records per page.

As the total number of records being fetched are more than 3, the nextLink attribute is retuned with the link to fetch the next set of records.

The value of the nextLink attribute returned is already encoded. Before we pass the link to fetch the next set of records we have to make sure to only set the query in the options parameter.

We also store all the values returned in a separate variable so that it can be used later.

Step 2: The code is shown below. The allaccounts variable will store all the accounts fetched at the end as we keep on concatenating the received results.

Code:

var query = "?$select=name";
var allaccounts = null;
var scripting = {
retrieveMultipleContacts() {

var url = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/accounts";
Xrm.WebApi.retrieveMultipleRecords("account", query, 3).then(
function success(result) {
var resultRetrieved = result;
allaccounts = resultRetrieved.entities.concat(allaccounts);

if (result.nextLink != undefined) {
console.log("Next page link: " + result.nextLink);
query = result.nextLink;
var splitValue = query.split(url);
query = splitValue[1];
scripting.retrieveMultipleContacts();
}

},
function (error) {
console.log(error.message);
}

);

}

}

Step 3: To test this out we can simply trigger this code to run on the change of form fields and while debugging we can check the values returned and stored in the allaccounts variable.

Conclusion:

The new D365 v9.0 Xrm.WebApi.retrieveMultipleRecords method simplifies the whole process of fetching records using paging.


Share Story :

Secured By miniOrange