Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript to update sum of field values

Posted on by 160

Hi,
I am trying to sum up few field values and update it to a field through below javascript code. I have added this function in all fields (stl_cpdu1, stl_cpdu2, etc.)...Is this correct?

/Function to add all CPDUs values
function Event_Update_TotalCPDU ()
{
var CalculatedCPDU=
Xrm.Page.getAttribute("stl_cpdu1").getValue() +
Xrm.Page.getAttribute("stl_cpdu2").getValue() +
Xrm.Page.getAttribute("stl_cpdu3").getValue() +
Xrm.Page.getAttribute("stl_cpdu4").getValue() +
Xrm.Page.getAttribute("stl_cpdu5").getValue() +

//Set value for Total CPDU
Xrm.Page.getAttribute("stl_cpdu").setValue(CalculatedCPDU);
}

But, I am getting below error message in the form after entering the value.

@xxxx.xxx.dynamics.com/.../ClientApiWrapper.aspx line 157 > eval:1:1
RunHandlerInternal@xxxx.xxx.dynamics.com/.../ClientApiWrapper.aspx
RunHandlers@xxxx.xxx.dynamics.com/.../ClientApiWrapper.aspx
ExecuteHandler@xxxx.xxx.dynamics.com/.../ClientApiWrapper.aspx
$Ce_1@xxxx.xxx.dynamics.com/.../formcontrols.js
executeHandler@xxxx.xxx.dynamics.com/.../formcontrols.js
executeHandlerByDescriptor@xxxx.xxx.dynamics.com/.../formcontrols.js
getHandler/<@xxxx.xxx.dynamics.com/.../formcontrols.js
getHandler/<@xxxx.xxx.dynamics.com/.../global.ashx
fireOnChange@https:/XXXX.XXX.dynamics.com/_static/form/formcontrols.js?ver=-385382622:12178:13
setValueInternal@xxxx.xxx.dynamics.com/.../formcontrols.js
setValue@xxxx.xxx.dynamics.com/.../formcontrols.js
$3e_2@https:/XXXX.XXX.dynamics.com/_static/form/formcontrols.js?ver=-385382622:25826:9
Function.createDelegate/<@XXXX.XXX.dynamics.com/_static/_common/scripts/MicrosoftAjax.js?ver=-385382622:24:16
b@xxxx.xxx.dynamics.com/.../MicrosoftAjax.js

Thanks,
Praveen.

*This post is locked for comments

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript to update sum of field values

    Hi Praveen ,

    Take a look below references -

    docs.microsoft.com/.../clientapi-form-context

    For passing execution context please have a look -

    neilparkhurst.com/.../javascript-prevent-save

    practical-crm.blogspot.com/.../passing-execution-context-to-onchange.html

  • RE: Javascript to update sum of field values

    Hi Shidin,

    I am not a developer in nature, so can you guide me what to use instead of Xrm.Page?

    Thanks,

    Praveen.

  • RE: Javascript to update sum of field values

    Hi Kawinski,

    I have updated ';' instead of +. The fields do have null value. How can I overcome this issue?

    Thanks,

    Praveen.

  • Verified answer
    G Kawinski Profile Picture
    G Kawinski 380 on at
    RE: Javascript to update sum of field values

    Looks like you also have a syntax error.  The last '+' should be ';'.  The browser might be having trouble interpreting the JS, or passing a null value.  

    Xrm.Page.getAttribute("stl_cpdu3").getValue() +

    Xrm.Page.getAttribute("stl_cpdu4").getValue() +

    Xrm.Page.getAttribute("stl_cpdu5").getValue() +

    //Set value for Total CPDU

    Xrm.Page.getAttribute("stl_cpdu").setValue(CalculatedCPDU);

    }

  • Verified answer
    Shidin Haridas Profile Picture
    Shidin Haridas 3,497 on at
    RE: Javascript to update sum of field values

    And regarding the code snippet posted above:

    - Please do verify that all the fields above have values.

    You have not implemented any null checks.

    - Xrm.Page is depreciated. You would need to use the form context to get the attribute values.

    - Use a try catch block in the script function.

    I do not see anything useful in the error message.

  • Suggested answer
    Shidin Haridas Profile Picture
    Shidin Haridas 3,497 on at
    RE: Javascript to update sum of field values

    Hi Praveen,

    Why don't you use calculated fields feature of CRM instead?

    https://technet.microsoft.com/en-us/library/dn832103.aspx

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