Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Calculated Rollup Fields

Posted on by

I have created a field on a form that is a questionnaire.  The questions are in a sub form. The user will answer Yes or No to each question and the rollup field will calculate the number of answers that have been answered Yes.  The field is working correctly, but it does not calculate immediately after I press save.  I have to click the refresh button next to the field and then the field calculates correctly.  Is there some way to configure the system to recalculate the rollups when the save button is clicked or when the record is changed as an answer is keyed?

*This post is locked for comments

  • Suggested answer
    Yusi Profile Picture
    Yusi 100 on at
    RE: Calculated Rollup Fields

    This problem requires two step solution.

    First you need to invoke the CalculateRollupField either via JS or C#.

    For your requirement, since you need to see the results immediately, I suggest use JavaScript.

    The following JS code will trigger your roll up field recalculation

    function CalculateRollupField(EntityName, recGuid, RollUpFieldName)

    {
    recGuid = recGuid.replace("{", "").replace("}", ""); // String Guid

    var HttpReq = new XMLHttpRequest();

    HttpReq.open(this.method, this.url, true); // Need this to initiate the request

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

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

    // Double Check your API URL, may differ for v9.0
    HttpReq.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + "CalculateRollupField(Target=@p1,FieldName=@p2)?" + "@p1={'@odata.id':'" + EntityName + "(" + recGuid + ")'}&" + "@p2='" + RollUpFieldName + "'", true);

    HttpReq.onreadystatechange = function ()

    {

    if (this.readyState === 4)

    {

    HttpReq.onreadystatechange = null;

    if (this.status === 200)

    {

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

    }

    else

    {

    Xrm.Utility.alertDialog(this.statusText);

    }

    }

    };

    HttpReq.send(JSON.stringify({}));

    }

    // You can then retrieve the updated value via JS API call (or fetchXML) & use XRM commands to replace the updated value in the form to display it to user right away 

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calculated Rollup Fields

    Hi,

    if you want to see it right away you will need to develop some client-side code, i.e. Javascript function to calculate your rollup filed.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Calculated Rollup Fields

    Hi,

     you can also try this kind of workflow solutions to calculate rollup fields:

    github.com/.../Dynamics-365-Workflow-Tools

     (actually, all those solutions iwll be using the request mentioned by Radu above.. but that's if you don't want to develop your own code)

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Calculated Rollup Fields

    Hi Tom,

    Rollup fields are calculated asynchronously - see msdn.microsoft.com/.../dn817863.aspx

    Seems there is a CalculateRollupField message that developers can use to calculate a rollup attribute value on demand - maybe that can help?

    There is also a similar thread, that has some further details community.dynamics.com/.../147140

    Another way that you can achieve a similar outcome, is to use Synchronous Plug-ins, instead of rollup fields - that perform this calculator.

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