+ Start a Discussion
SL TanSL Tan 

S-Control Code to sum up amounts in a custom object

Hi
I wonder if anyone can help me here. I have a custom object with a number of item amounts that I would like to use an S-control to sum up the amount as a Total Amount (Not using the Custom Formula for this case) Does anybody have a coding sample that can be shared with me, to select the item amounts and then push to sum them up?
Any pointer will be highly appreciated
Thanks in advance
SL
sfdcfoxsfdcfox
That's an easy one. Just use this code:
<script language="javascript" src="/soap/ajax/9.0/connection.js">
</script>

<script language="JavaScript">
ChildrenObjects = sforce.connection.query("select Id,AmountField__c from ChildObject__c where MasterRecord__c = '{!MasterObject__c.Id}'")
ChildObjectIterator = new sforce.QueryResultIterator(ChildrenObjects)
MasterObject = new sforce.SObject("MasterObject__c")
MasterObject["Id"] = "{!MasterObject__c.Id}"
MasterObject["TotalAmount__c"] = 0
while(ChildObjectIterator.hasNext())
{ ChildObject = ChildObjectIterator.next()
MasterObject["TotalAmount__c"] = MasterObject.getFloat("TotalAmount__c")+ChildObject.getFloat("AmountField__c")
}
sforce.connection.update([MasterObject])
</script>
If you're using a Button/Link, you may replace the red text with {!RequireScript("/soap/ajax/9.0/connection.js")}. Salesforce will include your JavaScript file with all the other header scripts on the page.

~ sfdcfox ~

SL TanSL Tan

Hi sfdcfox
Thanks for your help and coding sample which I appreciate very much. I did some modification and tested using the S-control but the result is a blank. Firebug indicates a Servlet Integration matter. I hope you can help me point out what is the problem if I append my code below. Sorry for any inconvenience caused
Many thanks in advance
SL
Code:
<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script language="JavaScript">

ChildrenObjects = sforce.connection.query("select Id,cmi__Col_1_Amt__c,cmi__Col_2_Amt__c,cmi__Col_3_Amt__c,cmi__Col_4_Amt__c,cmi__Col_5_Amt__c,cmi__Col_6_Amt__c,
cmi__Col_7_Amt__c,cmi__Col_8_Amt__c,cmi__Col_9_Amt__c,cmi__Col_10_Amt__c,
cmi__Col_11_Amt__c,cmi__Col_12_Amt__c from cmi__Production_Order__c where Id ={!cmi__Production_Order__c.Id}
ChildObjectIterator = new sforce.QueryResultIterator(ChildrenObjects)
MasterObject = new sforce.SObject("cmi__Production_Order__c")
MasterObject["Id"] = "{!cmi__Production_Order__c.Id}"
MasterObject["cmi__TotalAmount__c"] = 0
while(ChildObjectIterator.hasNext())
{ ChildObject = ChildObjectIterator.next()
  MasterObject["cmi__TotalAmount__c"] = MasterObject.getFloat("cmi__TotalAmount__c")+ChildObject.getFloat("cmi__Col_1_Amt__c”,  “cmi__Col_2_Amt__c” , “cmi__Col_3_Amt__c”, “cmi__Col_4_Amt__c”, “cmi__Col_5_Amt__c”, “cmi__Col_6_Amt__c”, “cmi__Col_7_Amt__c”,”cmi__Col_8_Amt__c”, “cmi__Col_9_Amt__c”,
“cmi__Col_10_Amt__c”, “cmi__Col_11_Amt__c”, “cmi__Col_12_Amt__c”)
}
sforce.connection.update([MasterObject])
</script>

 

rockchick322004rockchick322004
Is the relationship between the parent and the child a master-detail relationship?  If so, you can use the new  Summer '07 Roll-Up Summary Field feature (a new custom field type) to sum up the amount of the child into a field on the parent.
SL TanSL Tan

Hi
There is no master detail relationship in my case. Sorry I think my original post was not clear. All the item amounts are in the same object. I could not use Formula to add the amounts as it is beyond the compile limit so I am trying to use S-Control to push out the total
Thanks
SL
sean33043sean33043

Assuming the 12 amount fields default to 0, and you are trying to simulate a formula field,

Why not this:

Code:

<script src="/soap/ajax/9.0/connection.js"></script>
<script language="JavaScript">

var cmi_Id = "{!cmi__Production_Order__c.Id}";
var grandTotal = {!cmi__Col_1_Amt__c} + {!cmi__Col_2_Amt__c} + {!cmi__Col_3_Amt__c} + ...
MasterObject = new sforce.SObject("cmi__Production_Order__c")
MasterObject["Id"] = cmi_Id;
MasterObject["cmi__TotalAmount__c"] = grandTotal;
sforce.connection.update([MasterObject])
</script>


 

 

 

SL TanSL Tan

Hi Sean
Thanks for your kind advise. Appreciated
Best Regard
SL