Sales force provided standard roll up field facility in case of master detail relationship.However if you have lookup relationship and you want to built that functionality you need to built through trigger .
Below one example which has some generic API names needs to replace with specific .
1.Child_Object__c needs to replace with child object API .
2.Parent_Object__c needs to replace with parent object API
3.LookUpFld__c needs to replace with the lookup field API .
4.Parent_Fld_To__c needs to replace with the parent filed to which roll up amount will update .
5.Child_Fld_From__c Needs to replace with child field from the value will roll up .
trigger RollUpFromChildToParent on Child_Object__c ( after insert, after update,after delete,after undelete) {
Set<Id> prIdSet=new Set<Id>();
List<Parent_Object__c> pListToUpdate=new List<Parent_Object__c>();
if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
for(Child_Object__c cobj : Trigger.new){
if(cobj.LookUpFld__c != null)
prIdSet.add(cobj.LookUpFld__c);
}
}If(Trigger.isDelete){
for(Child_Object__c cobj : Trigger.old){
if(cobj.LookUpFld__c != null)
prIdSet.add(cobj.LookUpFld__c);
}
}
for(AggregateResult res : [SELECT LookUpFld__c,sum(Child_Fld_From__c)can FROM Child_Object__c WHERE LookUpFld__c IN :prIdSet GROUP BY LookUpFld__c]) {
pListToUpdate.add(new Parent_Object__c(Id=(Id)res.get('LookUpFld__c'),Parent_Fld_To__c=(Double)res.get('can')));
}
try{
update pListToUpdate;
}catch(DmlException de){
System.debug(de);
}
}
Below one example which has some generic API names needs to replace with specific .
1.Child_Object__c needs to replace with child object API .
2.Parent_Object__c needs to replace with parent object API
3.LookUpFld__c needs to replace with the lookup field API .
4.Parent_Fld_To__c needs to replace with the parent filed to which roll up amount will update .
5.Child_Fld_From__c Needs to replace with child field from the value will roll up .
trigger RollUpFromChildToParent on Child_Object__c ( after insert, after update,after delete,after undelete) {
Set<Id> prIdSet=new Set<Id>();
List<Parent_Object__c> pListToUpdate=new List<Parent_Object__c>();
if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
for(Child_Object__c cobj : Trigger.new){
if(cobj.LookUpFld__c != null)
prIdSet.add(cobj.LookUpFld__c);
}
}If(Trigger.isDelete){
for(Child_Object__c cobj : Trigger.old){
if(cobj.LookUpFld__c != null)
prIdSet.add(cobj.LookUpFld__c);
}
}
for(AggregateResult res : [SELECT LookUpFld__c,sum(Child_Fld_From__c)can FROM Child_Object__c WHERE LookUpFld__c IN :prIdSet GROUP BY LookUpFld__c]) {
pListToUpdate.add(new Parent_Object__c(Id=(Id)res.get('LookUpFld__c'),Parent_Fld_To__c=(Double)res.get('can')));
}
try{
update pListToUpdate;
}catch(DmlException de){
System.debug(de);
}
}