web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Getting same page records all time when use WebApi with script

(0) ShareShare
ReportReport
Posted on by 140

Hi All,

We use WebApi to retrieve all the account records with script. We check the response where we saw that we getting same records all time, therefore our retrieving process never ends.

We also try to retrieve with page cookie but no success, All time we get the same page cookie.

In account fetchXml, we have only one attribute i.e. Name.

Thanks in advance.

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Can you please provide your code?

  • Suggested answer
    mobeenmohsin Profile Picture
    on at

    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

  • thonda Profile Picture
    Microsoft Employee on at

    Hi Vikas,

    Please check if Mohsin's sample helps you resolve the issue, if not as Andrew asked for, please provide your script to understand the issue better.

    Regards,

    Praveen T

    D365 CE Support

    Microsoft Corporation

  • Vikas Negi Profile Picture
    140 on at

    Hi All,

    We tried the suggestion that provide by the Mobeen Mohsin, but it's does not work.

    We worked more on this issue and we found that we have ‘SDK Message Processing Steps’ registers with RetrieveMultiple SDK Message on the Account entity, so we deactivate the same and again check our code, its started working.

    Anyone know or facing such kind of issue when ‘SDK Message Processing Steps’ registers with RetrieveMultiple SDK Message on the Account entity.

    Thanks

    Vikas

  • mobeenmohsin Profile Picture
    on at

    Hi Vikas,

    Thank you for feedback.

    Normally, there should not be any issue with OOB plugins for RetrieveMulitple message. We suspect that there is some validation in plugins which is restricting your code to give you results. Please verify if you have custom plugins. Thank you.

    Best Regards,

    Mobeen Mohsin

  • Vikas Negi Profile Picture
    140 on at

    Thanks for reply.

    Yes we have custom plugin on Account entity. what you think, what will be the issue?

    Thanks

  • mobeenmohsin Profile Picture
    on at

    Hi Vikas,

    There might be some validation in custom plugin which is stopping to retrieve the records, if you can't figure out it is recommended to open the support request using admin.powerplatform.microsoft.com/. Thank you.

    Best Regards,

    Mobeen Mohsin

  • thonda Profile Picture
    Microsoft Employee on at

    Hi Vikas,

    It appears your Plugin Type registered on the RetrieveMultiple manipulating the fetchXml query impacting the results queried & sent back to UI.

    You can try to paste your plugin type code here if its not complex and not heavily dependent on libraries.

    Regards,

    Praveen T

    D365 Customizations Support

    Microsoft Corporation

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 71 Most Valuable Professional

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 66 Super User 2026 Season 1

#3
ManoVerse Profile Picture

ManoVerse 51 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans