Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Simple Math (JavaScript)

(0) ShareShare
ReportReport
Posted on by

I'm trying to learn JS and create a simple JS function to add up 8 fields and put the total into a variable TT and update a field Total Time with that variable.  

I have it set for onsave on the form but the field is not updating.  These are all whole number/duration fields.  Is there some problem with my JS?

function Form_OnSave() {
// All all of the duration fields in the form

var tt =
(Xrm.Page.getAttribute("new_billabletime").getValue() +
Xrm.Page.getAttribute("new_billabletraveltime").getValue() +
Xrm.Page.getAttribute("new_mfgrwarrantytime").getValue() +
Xrm.Page.getAttribute("new_lunchandbreaks").getValue() +
Xrm.Page.getAttribute("new_warrantytime").getValue() +
Xrm.Page.getAttribute("new_ojttime").getValue() +
Xrm.Page.getAttribute("new_billabletimeot").getValue() +
Xrm.Page.getAttribute("new_billabletraveltimeot").getValue());

Xrm.Page.getAttribute("new_totaltimeaccountedfor").setValue(tt);
}

Thank you

*This post is locked for comments

  • RE: Simple Math (JavaScript)

    Thank you Abi.

  • Verified answer
    AbiRami Profile Picture
    AbiRami 516 on at
    RE: Simple Math (JavaScript)

    Hi,

    To debug custom JavaScript code in CRM using browser developer tools:
    1. Add "debugger;" word in the start of your required function
    2. Refresh the CRM Page(Click F5)
    3. Open debugger tool(Click F12 or Right click->inspect)
    4. Refresh your CRM page once again
    5. Select the Sources tab in the developer tools
    6. Now whenever the function is called, control comes to the debugger
    7. Also u can add breakpoint by clicking on the line number on the left (adds a little blue marker)

    Then during execution debugging you can do a handful of stepping motions.

    F8 Continue: Will continue until the next breakpoint
    F10 Step over: Steps over next function call (won't enter the library)
    F11 Step into: Steps into the next function call (will enter the library)
    Shift + F11 Step out: Steps out of the current function

    Also refer the following link
    www.youtube.com/watch

    This method of debugging has slight difference. Instead of adding "debugger;" word in your code, search the required function in developer tools-> add breakpoints manually-> Refresh.

    Thanks

  • RE: Simple Math (JavaScript)

    Thank you Guido.  I thought my code looked a little fat but being a JS newbie, it's all about functionality before form for me.  I appreciate your input on this - your code works perfectly.  And W3Schools has been great.

    I've also found this: www.webtoolkitonline.com/javascript-tester.html

    And this: codebeautify.org/jsvalidate

    To be helpful in checking my code.

  • RE: Simple Math (JavaScript)

    Thank you all for the responses.  I realized after sending this that the script itself had no issues - this was a layer 8 issue, as it'd been a 16-hour day and I wasn't thinking straight.  :)

    A couple of you said 'use the debugger'.  Easy for you to say!  Which debugger?  Where is it?  

    Thank you

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Simple Math (JavaScript)

    Hi,

    If your new_totaltimeaccountedfor is disabled, the value will not be saved. You have to setSubmitMode for this field to "always".

  • Suggested answer
    Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 on at
    RE: Simple Math (JavaScript)

    Hi Daniel

    In that some of the control value is null or empty string. Is there any error throws?

  • Suggested answer
    AbiRami Profile Picture
    AbiRami 516 on at
    RE: Simple Math (JavaScript)

    yes. Its working fine. Use debugger to find the exact error. Refresh your page after save

    Thanks

  • Suggested answer
    ganeshm Profile Picture
    ganeshm on at
    RE: Simple Math (JavaScript)

    Hi daniel,

    i tried the same with two duration field it worked fine.

    I suggest you to try with a debugger; to find the error.I think you must have got one of the field name wrong. 
    Best run the code in the console of your browser developer tool to find the error faster.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans