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.