Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Re-calculate Roll-up field from JavaScript

Posted on by Microsoft Employee

Hello Experts,

I kind of know it is not possible, but is there any supported / unsupported way to force the form to recalculate a roll-up field through JavaScript. If not possible through JS what about through a plugin?

Looking forward to hear

AbD

*This post is locked for comments

  • CrmMad Profile Picture
    CrmMad 65 on at
    RE: Re-calculate Roll-up field from JavaScript

    it's not possible from JS, in my opinion you must create a normal number field and you calculate with js

  • Suggested answer
    Yusi Profile Picture
    Yusi 100 on at
    RE: Re-calculate Roll-up field from JavaScript

    Yes , the code below works perfect for me.

    function CalculateRollupField(EntityNamerecGuidRollUpFieldName)

    {
    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 and use XRM commands to replace the updated value in the form

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Re-calculate Roll-up field from JavaScript

    Hi Drew, thank you thank you it works! I didn't figure out how simple is. Thanks again, have a nice day. Walter

  • Suggested answer
    Drew Poggemann Profile Picture
    Drew Poggemann 9,079 on at
    RE: Re-calculate Roll-up field from JavaScript

    Hi Walter,

    The tool I mentioned has been rolled into Demian's Workflow tools I think...  This is what I have been using for past couple of implementations.

    https://github.com/demianrasko/Dynamics-365-Workflow-Tools

    The field name should be the "Name" of the field, not the schema name of the field.  This is actually a documentation fix I had them update recently.  

    https://github.com/demianrasko/Dynamics-365-Workflow-Tools/issues/12

    As far as the parent entity dynamics field, you should be able to select the Account entity and then there will be a field in the list called Record URL (Dynamic) which will point to it.

    Let me know if does not work.


    Thanks!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Re-calculate Roll-up field from JavaScript

    Hi drew, sorry if I'm bothering you, I'm experiencing your same needs but I'm not able to solve my problem.

    I have the entity "Credits" that have a field named Quantity. The parent entity is Account, that has a rollup field named CreditSum. All works fine, each Account's rollup field CreditSum is correctly updated (manually or every 12 hours) with the sum of the corresponding Credits.

    Now I'm trying to setup the Workflow like you did, that executes a rollup each time I add, modify, delete a record in the Credits table, but the Workflow stops with an error. I'm wasting hour and hours because I don't know what to enter to the fields Fieldname and Parent Record URL.

    Thank you for your help. Sorry for my poor english!

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: Re-calculate Roll-up field from JavaScript

    Yes. Your are right. But that can be handled through the coding logic like using flag variable or something like that. Also, I strongly recommend to not go for unsupported development.

    Also, there does not seem to be any supported way unless the one I mentioned in my earlier comment.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Re-calculate Roll-up field from JavaScript

    Thank you Mr. Yadnyesh,

    had thought of this but below are my issues:

    1) The reason I mentioned my CRM version (2015 on-premise) is that the subgrid control access in JavaScript is from CRM 2015 Update 1 onwards (which never got released for on-premise)

    2) Say, even if I do this using those unsupported methods, won't it go to an indefinite loop if I try to refresh the form during on-load of the grid? When the form is refreshed the grid will load once again and fire the on-load event which will cause the form to refresh again. (Haven't tried this, but theoretically am I correct) ?

    Thanks

    AbD

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: Re-calculate Roll-up field from JavaScript

    Hi,

    Its possible through code only. No other way is possible.

    You will have to add JavaScript function on the OnLoad event on grid control. Refer below link:

    https://msdn.microsoft.com/en-in/library/dn932126.aspx#BKMK_subgridAddOnLoad

    When everytime we create child record, grid gets refreshed and on load event gets triggered.

    In this function, you will have to refresh the form with below method:

    https://msdn.microsoft.com/en-in/library/dn481607.aspx#BKMK_refresh

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Re-calculate Roll-up field from JavaScript

    Any ideas how do I refresh the page after the child records are created? I tried, in the same workflow, update some fields of the parent record hoping that it would refresh. But it doesn't, event those changes are visible after a (manual) refresh. My CRM version 2015 so I cannot attach any events to the subgrid in a supported way either. Since the update I mentioned also doesn't show up in real time, I am unable to use on-change of any fields in the parent record as the changes doesn't show up immediately hence doesn't fire the event.

    I am trying to avoid the need of writing plugin for this purpose.

    Thanks

    AbD

  • Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: Re-calculate Roll-up field from JavaScript

    Yep. I agree. :P

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans