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
VICKY_SFDCVICKY_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
Best Answer chosen by VICKY_SFDC
mukesh guptamukesh gupta
Hi Vikas,

Please follow below code:

Apex Trigger:-
trigger UpdateParentAmount on emplayment__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);  
    }
}
Apex class:- UpdateCustomAmmount_Handler ​​​​​​​
public class UpdateCustomAmmount_Handler {
    public static void updateAmmount(List<emplayment__c> olt){
       
        List<Id> listIds = new List<Id>();
        system.debug('list of emplaymentids---'+listIds);
        
        for (emplayment__c childItem : olt) {
            listIds.add(childItem.Employee__c);
        }
        
          list<Employee__c> parentObj = new List<Employee__c>([SELECT id, Custom_Ammount__c, Name,(SELECT ID, TotalPrice FROM emplayments__r) FROM Employee__c WHERE ID IN :listIds]);
          system.debug('list of Employee with child---'+parentObj); 
          
        for(Employee__c pObj:parentObj){
            pObj.Custom_Ammount__c = 0;
            for(emplayment__c item:pObj.emplayments__r){
			    Decimal tempPrice = (item.TotalPrice != null) ? item.TotalPrice : 0 ;	
                pObj.Custom_Ammount__c += tempPrice; 
               }
            }
        If(parentObj.size()>0){
            update parentObj;  
        }   
    }
}

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

CharuDuttCharuDutt
Hii VikasKumar
Try Below Trigger
trigger NumberOfChild on emplayment__c(After Insert,After Update,After Delete,After Undelete) {
    
  List<Employee__c> accList=new List<Employee__c>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(emplayment__c con : Trigger.new){
            if(con.Employee__c != null){
            setAccIds.add(con.Employee__c);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(emplayment__c con : Trigger.new){ 
            if(con.Employee__c !=Trigger.oldMap.get(con.Id).Employee__c){
               	setAccIds.add(con.Employee__c);
                setAccIds.add(Trigger.oldMap.get(con.Id).Employee__c);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(emplayment__c con : Trigger.old) { 
            if(con.Employee__c != null){
            setAccIds.add(con.Employee__c);
            	}
        	}
        }
    }   
    if(Trigger.isUndelete){
        if(trigger.isAfter){
        for(emplayment__c con : Trigger.new) { 
            if(con.Employee__c != null){
            setAccIds.add(con.Employee__c);
            	}
        	}
        }
    }   
 
    for(Employee__c acc :[Select id,Total_Amount__c,(Select id,name,Amount__c from emplayments__r ) from Employee__c where Id in : setAccIds]){
      integer val = 0;
        for(emplayment__c con : acc.emplayments__r ){
            
            val += integer.valueOf(con.Amount__c);
            system.debug('====> ' +val);
        }
        system.debug(val);
        acc.Total_Amount__c = string.valueOf(val);
        
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
mukesh guptamukesh gupta
Hi Vikas,

Please follow below code:

Apex Trigger:-
trigger UpdateParentAmount on emplayment__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);  
    }
}
Apex class:- UpdateCustomAmmount_Handler ​​​​​​​
public class UpdateCustomAmmount_Handler {
    public static void updateAmmount(List<emplayment__c> olt){
       
        List<Id> listIds = new List<Id>();
        system.debug('list of emplaymentids---'+listIds);
        
        for (emplayment__c childItem : olt) {
            listIds.add(childItem.Employee__c);
        }
        
          list<Employee__c> parentObj = new List<Employee__c>([SELECT id, Custom_Ammount__c, Name,(SELECT ID, TotalPrice FROM emplayments__r) FROM Employee__c WHERE ID IN :listIds]);
          system.debug('list of Employee with child---'+parentObj); 
          
        for(Employee__c pObj:parentObj){
            pObj.Custom_Ammount__c = 0;
            for(emplayment__c item:pObj.emplayments__r){
			    Decimal tempPrice = (item.TotalPrice != null) ? item.TotalPrice : 0 ;	
                pObj.Custom_Ammount__c += tempPrice; 
               }
            }
        If(parentObj.size()>0){
            update parentObj;  
        }   
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 


 
This was selected as the best answer
VICKY_SFDCVICKY_SFDC
@Thanks Charu For your Trigger code.
VICKY_SFDCVICKY_SFDC
@Mukesh Thanks for your code,,,i undersand the logic and implemented 
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);  
    }
}