Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Getting same page records all time when use WebApi with script

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.

  • thonda Profile Picture
    thonda on at
    RE: Getting same page records all time when use WebApi with script

    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

  • mobeenmohsin Profile Picture
    mobeenmohsin on at
    RE: Getting same page records all time when use WebApi with script

    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

  • Vikas Negi Profile Picture
    Vikas Negi 140 on at
    RE: Getting same page records all time when use WebApi with script

    Thanks for reply.

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

    Thanks

  • mobeenmohsin Profile Picture
    mobeenmohsin on at
    RE: Getting same page records all time when use WebApi with script

    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
    Vikas Negi 140 on at
    RE: Getting same page records all time when use WebApi with script

    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

  • thonda Profile Picture
    thonda on at
    RE: Getting same page records all time when use WebApi with script

    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

  • Suggested answer
    mobeenmohsin Profile Picture
    mobeenmohsin on at
    RE: Getting same page records all time when use WebApi with script

    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

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Getting same page records all time when use WebApi with script

    Hello,

    Can you please provide your code?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans