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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Asynchronous XmlHTTPrequest and Retrieve Multiple Issue

(1) ShareShare
ReportReport
Posted on by 40

Hi everybody,

I've been trying to figure this out on my own, but honestly my knowledge of asynchronous requests is thin at best.

Here is the context-- I have too many rollup fields on an entity, so I'm trying to create a custom rollup field using javascript.

I have a formOnLoad function that says: if "example" field is empty, loop through members of a related entity and aggregate financial information, then populate the "example" field.

In the past, with async functions, when I have the .setValue() function outside the XML request, sometimes it executes too early and will not populate the field with the correct number (in situations where I am looping through response data and aggregating values from that data).

What I can't figure out is how to ONLY use the formContext.getAttribute().setValue() function when the for loop inside the XmlHTTPrequest (which is looping through records in the JSON response and adding values to the "ttl" (total) field, a variable defined outside the scope of this function). When I try to use either formContext.getAttribute... or Xrm.Page.getAttribute... within the "onreadystatechange" function, or when I try to embed it in a callback function (in red and bolded below), I get an error:

Xrm.Page.getAttribute(...).setAttribute is not a function
at XMLHttpRequest.req.onreadystatechange

I am assuming this happens because the formContext or globalContext is not being passed to the onreadystatechange function-- but I could be so off. If someone understands my issue, I would be grateful for help in fixing it. If there are any questions, please let me know.

                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        var results = JSON.parse(this.response);
                        for (var i = 0i < results.value.lengthi++) {
                            var example= results.value[i]["example"];
                            var example_formatted= results.value[i]["example@OData.Community.Display.V1.FormattedValue"];
                            ttl += example;
                            console.log(ttl);
                        }
                        setCallback(ttl,remRecoverable); / Xrm.Page.getAttribute("example").setValue(ttl); / formContext.getAttribute("example").setValue(ttl);
                    } else {
                        Xrm.Utility.alertDialog(this.statusText);
I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Asynchronous XmlHTTPrequest and Retrieve Multiple Issue

    Hello,

    First recommendation - don't use JS for this purposes.

    Just imagine that child record is created from Workflow or created by other user on his/her laptop. And the answer here - use OOB Rollup fields or plugin to handle create/update/delete in the proper way.

    If you anyway want to make it work from JS - please provide the full code.

  • WhosComingforSupport Profile Picture
    40 on at
    RE: Asynchronous XmlHTTPrequest and Retrieve Multiple Issue

    Hi Andrew!

    Thanks so much for your response.

    I'm okay with not using JS for this, but OOB Rollup fields are out of the question-- we already have 10 on this entity.

    I still haven't learned how to build a plugin for CRM, so I think that's my next step. JS is the only recourse I really have right now to fix this in the short term. Maybe I just need to accelerate my C# study..

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Asynchronous XmlHTTPrequest and Retrieve Multiple Issue

    As I mentioned - if you want to fix your JS - post the full code here.

  • WhosComingforSupport Profile Picture
    40 on at
    RE: Asynchronous XmlHTTPrequest and Retrieve Multiple Issue

    Hi Andrew,

    Here it is- thank you.

    var Sdk = window.Sdk || {};

    (function () {

    this.formOnLoad = function(executionContext)

    {

       formContext = executionContext.getFormContext();

       formItem = formContext.ui.formSelector.getCurrentItem();

       fid = formItem.getId();

       console.log(fid);

       if(fid == "correct fid"){

           var recoverable  = formContext.getAttribute("recoverable").getValue();

           console.log(remRecoverable);

           if(recoverable != null){  

               var dirtyGuid = formContext.data.entity.getId();

               var currentGuid = dirtyGuid.slice(1,-1).toLowerCase();

               var req = new XMLHttpRequest();

               var total = 0;

               req.open("GET", Xrm.Page.context.getClientUrl() + "properstring", true);

               req.setRequestHeader("OData-MaxVersion", "4.0");

               req.setRequestHeader("OData-Version", "4.0");

               req.setRequestHeader("Accept", "application/json");

               req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

               req.onreadystatechange = function() {

                   if (this.readyState === 4) {

                       req.onreadystatechange = null;

                       if (this.status === 200) {

                           var results = JSON.parse(this.response);

                           for (var i = 0; i < results.value.length; i++) {

                               var example = results.value[i]["example"];

                               var example_formatted = results.value[i]["example@OData.Community.Display.V1.FormattedValue"];

                               total += example;

                           }

                           if(total != recoverable){

                               Xrm.Page.getAttribute("recoverable").setAttribute(total);

                               OR

                               formContext.getAttribute("recoverable").setAttribute(total);

                               }

                       } else {

                           Xrm.Utility.alertDialog(this.statusText);

                       }

                   }

               };

               req.send();    

               }

           }else if(remRecoverable == null){

               if(remRecoverable != null){  

                   var dirtyGuid = formContext.data.entity.getId();

                   var currentGuid = dirtyGuid.slice(1,-1).toLowerCase();

                   var req = new XMLHttpRequest();

                   var ttl = 0;

                   req.open("GET", Xrm.Page.context.getClientUrl() + "properstring", true);

                   req.setRequestHeader("OData-MaxVersion", "4.0");

                   req.setRequestHeader("OData-Version", "4.0");

                   req.setRequestHeader("Accept", "application/json");

                   req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

                   req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

                   cleanSet = function(callback){

                   req.onreadystatechange = function() {

                       if (this.readyState === 4) {

                           req.onreadystatechange = null;

                           if (this.status === 200) {

                               var results = JSON.parse(this.response);

                               for (var i = 0; i < results.value.length; i++) {

                                   var example = results.value[i]["example"];

                                   var example_formatted = results.value[i]["example@OData.Community.Display.V1.FormattedValue"];

                                   total += example;

                               }

                               Xrm.Page.getAttribute("recoverable").setAttribute(total);

                               OR

                               formContext.getAttribute("recoverable").setAttribute(total);

                           } else {

                               Xrm.Utility.alertDialog(this.statusText);

                           }

                       }

                   };

                   req.send();

               }

           }

       }

    }

    }).call(Sdk);

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Asynchronous XmlHTTPrequest and Retrieve Multiple Issue

    Try to use

    Xrm.Page.getAttribute("recoverable").setValue(total);

    OR

    formContext.getAttribute("recoverable").setValue(total);

    instead.

  • WhosComingforSupport Profile Picture
    40 on at
    RE: Asynchronous XmlHTTPrequest and Retrieve Multiple Issue

    Oh boy do I ever feel stupid. Guess I'm working with only a single braincell today.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 247

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 166 Super User 2025 Season 2

#3
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 164

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans