Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How we can calculate sum the amount that we got in the form of array based on some condition

Posted on by 70

Hello ,

I am new to crm dynamics 365 javascripts.

How we can calculate sum the amounts that we got in the form of array ?

for (var i = 0; i < results.value.length; i++)
{
var totalamount = results.value[i]["totalamount"];  // contains value like 300, 545, 500 etc
var chargetype = results.value[i]["chargetype"];     // contains charge type value like 1234,  3456, 1234 etc

}

I want sum of total amount whose charge type = 1234 (some value of the option set)

How we can achive this using javascript?

Thank you.

*This post is locked for comments

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: How we can calculate sum the amount that we got in the form of array based on some condition

    If you want to get the total of all the 1234 charge types, please try the following script and see if it works for you

    var totalamount  = 0;

    results.value.forEach(function(obj)

    {

       if (obj["chargetype"] === 1234)

           totalamount += obj["totalamount"];

    });

    Depending on how you retrieved, the data, you may have to do obj["chargetype"].value and obj["totalamount"].value to get it working

    Hope this helps

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: How we can calculate sum the amount that we got in the form of array based on some condition

    As an alternative, have you considered using a lookup field for the calculation, which avoids the need for code. This does have some limitations (e.g. it is only recalculated every hour by default, though there are tools to trigger a recalculation via a workflow)

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: How we can calculate sum the amount that we got in the form of array based on some condition

    Hi Suraj,

    Within your for loop create an array and add values to that. Try to explore a bit push() method.

    post that you can get the sum of those value from array : stackoverflow.com/.../how-to-find-the-sum-of-an-array-of-numbers  

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