function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
scottskiblackscottskiblack 

S-Control to Sum Amounts from Opportunities

Hello,

I am trying to sum the amounts from a group of Opportunities.  But I get a NaN (Not a Number) returned when trying to total up the amounts.  See code below.  I have tried to parse to a float, number, etc. but with no luck.  What am I missing?

Thanks!

<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
dojo.addOnLoad(init);

function init() {
var callback = {
onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Amount FROM Opportunity WHERE CloseDate = THIS_YEAR AND IsWon = true AND Probability = 100", callback);
}

function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
var totalAmount;

while(it.hasNext()) {
var record = it.next();
totalAmount += parseFloat(record.Amount);
html.push("Amount = " + record.Amount + "<br>");
html.push("<hr>");
html.push("<br>");
}
alert(totalAmount);
document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>


</head>
<body>

<div id="output-div"></div>

</body>
</html>
cheenathcheenath
    Try this:

  totalAmount += record.getFloat("Amount");


HTHs,




SuchitraSuchitra
Hi Scott,

I have made a small modification on your code

1. Assign totalAmount  = 0
2. Donot parse directly the record.Account object
3. Assign record.Account object to a var and then parse to float

<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
dojo.addOnLoad(init);

function init() {
var callback = {
onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Amount FROM Opportunity WHERE CloseDate = THIS_YEAR AND IsWon = true AND Probability = 100", callback);
}

function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
var totalAmount=0 ;

while(it.hasNext()) {
var record = it.next();

var amnt = record.Amount;
totalAmount += parseFloat(amnt);
html.push("Amount = " + amnt + "<br>");
html.push("<hr>");
html.push("<br>");
}
html.push("Total Amount : " + totalAmount);
document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>


</head>
<body>

<div id="output-div"></div>

</body>
</html>



Regards
P Suchitra :smileyhappy:
scottskiblackscottskiblack
thanks! :smileyvery-happy:
SL TanSL Tan
HI Suchitra
I copied your code given below to Scott and have successfully got the output for my custom object Production Order by modifying your coding to the relevant fields. In place of Opportunities, I have Production Order. In place of Amount, I have Prod Order Qty. So each Production Order has a Prod Order Qty. So I had the output showing the all the Production Order Qties and the Cumulative Prod Order Qty (ie the Total). However I now have a complication setting in. I can have several Production Orders to one Contract. I would now like to filter to capture only the Prod Order Qties for those Production Orders belonging to only one Contract. I tried various ways to capture the filter without success and the result output for Cumulative Prod Order Qty always show = 0 although no errors are reported. Do you have any idea how I should build in this filter? Hope you have come across a similar scenario and can advise me. Thanks a lot in advance. I append my code below too
Best Regards
SL
Code


<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
dojo.addOnLoad(init);

function init() {
var callback = {
onSuccess : displayResult,
onFailure : displayError
};
sforce.connection.query("SELECT Prod_Order_Qty__c FROM Production_Order__c ", callback);
}

function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
var totalProd_Order_Qty__c=0 ;

while(it.hasNext()) {
var record = it.next();

var amnt = record.Prod_Order_Qty__c;
totalProd_Order_Qty__c += parseFloat(amnt);
html.push("Prod Order Qty = " + amnt + "
");
html.push("
");
html.push("
");
}
html.push("Cumulative PO Qty : " + totalProd_Order_Qty__c);
document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
document.getElementById("output-div").innerHTML =
"oops something went wrong ... " + error;
}
</script>