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
Srikant Swain 8Srikant Swain 8 

Batch Class to calculate percentage on child records amount field and populate it on a custom object

I need to write a batch class monthly that will query all child records and calculates the percentage consumed from an amount field and creates a new record in a 3rd Object.
Example:- Object "Program" has multiple children "projects"(can have more than 200) with lookup relation and has a field "Amount". The parent "Program" object has a field  "Allocated Total Amount". I have to query all children and calculate the sum of the amount and take percentages like (Sum of Amount/ Allocated Total Amount * 100). After that, I have to create a record in another object "Summary Table" with Program Name, Start Date, and End Date from the Program Object and calculated the Percentage in a field.
Best Answer chosen by Srikant Swain 8
Karthik PadmakumarKarthik Padmakumar
Hi Srikant, 

I believe you can take the below approach.
You can have a stateful variable to calculate the required amount, by implementing Database.Stateful in the batch class.
In the Finish method of the batch, the insertion to the 3rd object can be done by taking the amount value from the stateful variable. 

Thanks.

All Answers

Karthik PadmakumarKarthik Padmakumar
Hi Srikant, 

I believe you can take the below approach.
You can have a stateful variable to calculate the required amount, by implementing Database.Stateful in the batch class.
In the Finish method of the batch, the insertion to the 3rd object can be done by taking the amount value from the stateful variable. 

Thanks.
This was selected as the best answer
denis banksdenis banks
Thats graet keep it up... https://www.smione.ltd/