Hi Vikas,
I think you don't have right fectXML because we need to mentioned distinct='false' in fetchXML in order to get correct paging cooking, please find verified code for reference:
// JavaScript source code
var serverUrl = Xrm.Page.context.getClientUrl();
function getAccounts() {
var fetchAccountXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='3' page='1'>";
fetchAccountXml += "<entity name = 'account'>";
fetchAccountXml += "<attribute name='name' />";
fetchAccountXml += "<order attribute = 'name' descending = 'false' />";
fetchAccountXml += "</entity >";
fetchAccountXml += "</fetch>";
fetchAccountXml = encodeURI(fetchAccountXml);
var accountsQueryURL = serverUrl + "/api/data/v9.1/accounts?fetchXml=" + fetchAccountXml;
var retrievedAccounts = getallRecords(accountsQueryURL);
alert(retrievedAccounts.results.length);
}
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;
// Initially defining the PageNumber
var pageNumber = 1;
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.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]);
}
alert(parsedResults["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"]);
// check if there are more records and set the new url, otherwise we set to null the url
if (parsedResults["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != null
&&
parsedResults["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != 'undefined') {
pageNumber++;
// Updating Query with page number to fetch Next set of records.
queryUrl = queryUrl.replace("page='" + (pageNumber - 1) + "'", "page='" + pageNumber.toString() + "'")
}
else {
queryUrl = null;
}
}
}
else {
alert(httpRequest.responseText);
// if the request has errors we stop and return a null result
queryUrl = null;
allRecords = null;
}
}
return allRecords;
}
Please help to mark this answer as verified if it solve your issue.
Best Regards,
Mobeen Mohsin