Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

JS - GP Calculation code check

(0) ShareShare
ReportReport
Posted on by 195

I'm getting a event:OnSave error with the following snippet

function Form_onsave()
{
     var GP = Xrm.Page.getAttribute("new_gp")
     var QTY = Xrm.Page.getAttribute("quantity")
     var PricePerUnit = Xrm.Page.getAttribute("priceperunit")
     var CostPerUnit = Xrm.Page.getAttribute("new_costperunit")

    GP.setValue
  (
        (
            (parseFloat(PricePerUnit.getValue() != null ? PricePerUnit.getValue() : 0))-(CostPerUnit.getValue() != null ? CostPerUnit.getValue() : 0))
        )*(QTY.getValue() != null ? QTY.getValue() : 0))
  )
}

Its telling me

Error:unable to get property 'getValue' of undefined or null reference.

anybody spot what my eyes are blind to?

*This post is locked for comments

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: JS - GP Calculation code check

    Hi Dave,

    Please try prepopulate your values. This will make the code cleaner and easier to debug:

    var quantity = Xrm.Page.getAttribute("quantity").getValue();

    var pricePerUnit = Xrm.Page.getAttribute("priceperunit").getValue();

    var costPerUnit = Xrm.Page.getAttribute("new_costperunit").getValue();

    var calculation = (pricePerUnit - costPerUnit) * quantity;

    Xrm.Page.getAttribute("new_gp").setValue(calculation)

    You can add code to validate your fields if you want, but having your code in this manner, will help you find out if there are any errors.

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: JS - GP Calculation code check

    Hi Dave,

    first thins is i didnt see you have put ";" after your lines.

    second  - what if you actually get the values as soon as you defined variables?

    var CostPerUnit = Xrm.Page.getAttribute("new_costperunit").getValue();

    and use CostPerUnit variable only?

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