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
VempallyVempally 

Required Trigger on child accessing parent field and updating parent.

Hi Everyone,

Can anyone give the code and explanation where i need to udate parent field with the value calculated with child fields.

Eg: Rollup Summary for Sum(). Need trigger code fro this for understanding.

All Answers

Abhishek Singh 88Abhishek Singh 88
there are different ways and scenario for updating records,could  you eleborate requirment. 
VempallyVempally
Hi Abhishek,

I have a field called amount__c on child and total_amount__c on parent.

total_amount__c takes the sum of all the values in amount__c in child.

 
Mahesh DMahesh D
Hi Vempally,

To calculate Rollup Summary using the Trigger manually, you need to follow below:

Need to consider all DML operations like insert, update, delete and undelete of child records.

Insert of Child record:
     --> Need to write an after insert trigger to update the Parent record with calculated sum value.
Update of Child record:
     --> Make sure that the Parent information got changed.
     --> Need to write an after update trigger and get both old and new Parent information and perform the re-calculation of sum.
Delete of Child record:
     --> Need to write an after delete trigger to update the Parent record with re-calculated sum value.
Undelete of Child record:
     --> Need to write an after undelete trigger to update the Parent record with re-calculated sum value.

Regards,
Mahesh