You need to sign in to do that
Don't have an account?

Calculate Sum of Amounts from child records using trigger?
Can anyone tell me how to write trigger for this scenario?
Two objects are in lookup relationship - Department (parent) and Employee . Employee has "Amount" field and Department has "Total Amount ". I need to write trigger to calculate sum of "Amount" of child records and display it on Dept. object.
Two objects are in lookup relationship - Department (parent) and Employee . Employee has "Amount" field and Department has "Total Amount ". I need to write trigger to calculate sum of "Amount" of child records and display it on Dept. object.
more details here:
https://developer.salesforce.com/trailhead/point_click_business_logic/roll_up_summary_fields
If you have any specific reason to use Trigger, then following may come handy for you:
Select it as Best Answer, if it solves your problem
All Answers
If you had used Master-Detail relationship between the 2 objects, then you could have used Roll up summary field to calculate the Total Amount instead of using a trigger.
In case you need a trigger then, you need to write the trigger on the amount object for all 3 cases, such as insert, update and delete.
trigger AmountTrigger on Emaployee__c (after insert, after update, after delete) {
Set<Id> DepartmentIds = new Set<Id>();
if(Trigger.isInsert || Trigger.isUpdate){
for( Employee__c emp : Trigger.New){
if(Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(emp.ID).Amount__c != emp.Amount__c)){
DepartmentIds.add(emp.Department__c);
}
}
if(Trigger.isDelete){
for( Employee__c emp : Trigger.Old){
DepartmentIds.add(emp.Department__c);
}
}
Map<Id, Decimal> TotalAmountMap = new Map<Id,Decimal>():
for(Employee__c emp : [select Id, Amount__c, Department__c from Employee__c where Department__c IN: DepartmentIDs]){
Decimal TotalAmount = 0;
if(TotalAmountMap.containsKey(emp.Department__c)
TotalAmount = TotalAmountMap.get(emp.Department__c);
TotalAmount = TotalAmount + emp.Amount__c;
TotalAmountMap.put(emp.Department__c, TotalAmount);
}
List<Department__c> updateDepartments = new List<Department__c>();
for(Id deptId : TotalAmountMap.keySet(){
Department__c dept = new Department__c(Id = deptID);
dept.Total_Amount__c = TotalAmountMap.get(deptId);
updateDepartments.add(dept);
}
If(updateDepartments.size() > 0)
Update updateDepartments;
}
Regards,
Prakash B
more details here:
https://developer.salesforce.com/trailhead/point_click_business_logic/roll_up_summary_fields
If you have any specific reason to use Trigger, then following may come handy for you:
Select it as Best Answer, if it solves your problem