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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

WebAPi rollupfields

(0) ShareShare
ReportReport
Posted on by 515

Does anyone know of sample code to update the rollupfields (in the current entity) using Web Api?

I've got the basic pages about it, but could do with a sample to get myself going in the right direction.

Thanks

Lee

I have the same question (0)
  • Verified answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hi,

    please refer to my sample code: 

    function relaclulateRollupField(executionContext, FieldName) {
        var formContext = executionContext.getFormContext();
        var recordId = formContext.data.entity.getId().replace("{", "").replace("}", "");
        var entityLogicalName = formContext.data.entity.getEntityName();
        var req = new XMLHttpRequest();
        console.log(calculatedRollupFieldReq);
        req.open("GET", Xrm.Utility.getGlobalContext().getClientUrl()   '/api/data/v9.1/CalculateRollupField(Target=@Target,FieldName=@FieldName)?@Target={"incidentid":"' recordId '","@odata.type":"Microsoft.Dynamics.CRM.' entityLogicalName '"}&@FieldName=\'' FieldName '\'', 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.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    //refresh
                    var results = JSON.parse(this.response);
                    formContext.data.refresh(false);
                    console.log(results)
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }

    req example: 

    /api/data/v9.1/CalculateRollupField(Target=@Target,FieldName=@FieldName)?@Target={"incidentid":"3AD95420-433A-EB11-A813-002248069B28","@odata.type":"Microsoft.Dynamics.CRM.incident"}&@FieldName="uma_rollupfieldexample"

    My code is executed when the form is loaded

  • Lee-Martin Profile Picture
    515 on at

    Thank you, I'm having an issue with it, could it be because I'm doing this with dates?

  • meelamri Profile Picture
    13,216 User Group Leader on at

    Can you give more details ? Please, share some screenshots.

  • Lee-Martin Profile Picture
    515 on at

    The rollups are all date fields, and need to be timestamped to whenever the page is opened

  • Lee-Martin Profile Picture
    515 on at

    I'm getting a 400 error with it

  • meelamri Profile Picture
    13,216 User Group Leader on at

    Hi,

    I just tested my Script with a date rollup, it works without any problem. Can you please share your code and how you configure your handle, thank you :)

  • Lee-Martin Profile Picture
    515 on at

    function recaclulateRollupField(executionContext) {
        var formContext = executionContext.getFormContext();
        var recordId = formContext.data.entity.getId().replace("{", "").replace("}", "");
        var entityLogicalName = formContext.data.entity.getEntityName();
    	
    
    	var lastReviewRollup = "can_lastreview";
    	console.log("Lee - "   lastReviewRollup);
    	
    	
        var req = new XMLHttpRequest();
      //  console.log(calculatedRollupFieldReq);
        req.open("GET", Xrm.Utility.getGlobalContext().getClientUrl()   '/api/data/v9.2/CalculateRollupField(Target=@Target,FieldName=@FieldName)?@Target={"incidentid":"' recordId '","@odata.type":"Microsoft.Dynamics.CRM.' entityLogicalName '"}&@FieldName=\'' lastReviewRollup '\'', 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.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    //refresh
                    var results = JSON.parse(this.response);
                    formContext.data.refresh(false);
                    console.log(results)
                }
                else if (this.status === 400) {
                    //refresh
                    console.log("Lee - failure");
    				console.log("Lee - "   this.statusText);
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }

  • Lee-Martin Profile Picture
    515 on at

    I think I've got it, I've left some code dependant on your system, where i should change it that on mine.

  • Lee-Martin Profile Picture
    515 on at

    Yep, put the right id in instead of incidentid in your code and it works fine

    Thanks for your help

  • meelamri Profile Picture
    13,216 User Group Leader on at

    You are welcome.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans