You need to sign in to do that
Don't have an account?
VICKY_SFDC
ROLL UP USING TRIGGER
SENARIO:
Employee is parent and emplayment is child both are custom and lookup r/s.
Now In child there is a Amount field(Currency field ) is there now assume c1,c2,c3 amount field is 1000 of each,,,,the toal of amount should be reflect on parent field i.e Total(Currency field with default value 0).
Now i want to rollup should work for all these actions insert,update,delete,Undelete
Employee is parent and emplayment is child both are custom and lookup r/s.
Now In child there is a Amount field(Currency field ) is there now assume c1,c2,c3 amount field is 1000 of each,,,,the toal of amount should be reflect on parent field i.e Total(Currency field with default value 0).
Now i want to rollup should work for all these actions insert,update,delete,Undelete
Please follow below code:
Apex Trigger:- Apex class:- UpdateCustomAmmount_Handler
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
All Answers
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
Please follow below code:
Apex Trigger:- Apex class:- UpdateCustomAmmount_Handler
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Below Is My Code
Parent-->Employee__c
Fields:
TotAL aMOUNT(cURRENCY): junebatch2021__Total__c[DEFAULT VALUE SET AS 0]
Child--->junebatch2021__EMP_PAYMENT__c [LOOKUP R/S]
AMOUNT(cURRENCY):: junebatch2021__EMP_AMOUNT__c
HANDLER:
public class UpdateCustomAmmount_Handler {
public static void updateAmmount(List<junebatch2021__EMP_PAYMENT__c> olt){
List<Id> listIds = new List<Id>();
system.debug('list of emplaymentids---'+listIds);
for (junebatch2021__EMP_PAYMENT__c childItem : olt) {
listIds.add(childItem.Employee__c);
}
list<Employee__c> parentObj = new List<Employee__c>([SELECT id, junebatch2021__Total__c, Name,(SELECT ID, junebatch2021__EMP_AMOUNT__c FROM EMP_PAYMENT__r) FROM Employee__c WHERE ID IN :listIds]);
system.debug('list of Employee with child---'+parentObj);
for(Employee__c pObj:parentObj){
pObj.junebatch2021__Total__c = 0;
for(junebatch2021__EMP_PAYMENT__c item:pObj.EMP_PAYMENT__r){
Decimal tempPrice = (item.junebatch2021__EMP_AMOUNT__c != null) ? item.junebatch2021__EMP_AMOUNT__c : 0 ;
pObj.junebatch2021__Total__c += tempPrice;
}
}
If(parentObj.size()>0){
update parentObj;
}
}
}
TRIGGER:
trigger UpdateParentAmount on junebatch2021__EMP_PAYMENT__c (after insert, after update, after Undelete,after delete) {
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUnDelete))
{
UpdateCustomAmmount_Handler.updateAmmount(Trigger.new);
}
if(Trigger.isAfter && Trigger.isDelete){
UpdateCustomAmmount_Handler.updateAmmount(Trigger.old);
}
}