You need to sign in to do that
Don't have an account?
Andrew King
need to write trigger that compiles sum of number field on mulitiple order records, and updates sum with each existing order record edit
hello, I'd like to create a trigger that compiles the sum of a number field on all order records, and updates the sum with each subsequent edit of the existing field on a record. We need a running sum of the number field on all orders, and it needs to update every time we edit the number field on existing orders. Any suggestions on how to write this trigger.
Example: we have number field A with a value of 10 on order 1. we have number field A with a value of 20 on order 2. The sum is 30. we go back and edit the number field on order 2 from 20 to 30. we need the sum to change from 30 to 40. instead it is changing from 30 to 60. we need it to replace the old value with the new, not add the new value to the old.
Any suggestions will be greatly appreciated.
Example: we have number field A with a value of 10 on order 1. we have number field A with a value of 20 on order 2. The sum is 30. we go back and edit the number field on order 2 from 20 to 30. we need the sum to change from 30 to 40. instead it is changing from 30 to 60. we need it to replace the old value with the new, not add the new value to the old.
Any suggestions will be greatly appreciated.
AggregateResult[] groupedResults
= [SELECT SUM(NumberField) totalSum FROM order_object];
Decimal totalOrders = Decimal.valueOf (groupedResults[0].get('totalSum'));
to get the toal sum and update your running sum field.
Have a look at http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm