Hey guys, today in this post we are going to learn about How to create roll-up summary trigger for count child records on custom object using Apex trigger in Salesforce.
parentObjTrigger:- Custom Fields & Relationships
Create a custom field on parent Object name as childRollUp__c (Number Type).
childObjTrigger:- Custom Fields & Relationships
Create a lookup relationship field on child Object name as childLookup__c : Lookup(parentObjTrigger).
Real time scenarios:- Write a roll-up summary trigger for count child records on custom parent object.
Create a custom field (Number Type) on parent object, calculate the total number of related child records and put into them.
Files we used in this post example:-
childObjTriggerRollUp.apxt | Apex Class Trigger | It will be fired whenever insert, update and delete a record on child object |
Parent Cusotm Object:- parentObjTrigger__c Custom Field :- childRollUp__c
|
Parent Object with Custom Field | Trigger on child object and calculate the total numberof related child records on the parent object. |
Child Custom Object:- childObjTrigger__c Custom Field:- childLookup__c >> Lookup(parentObjTrigger) |
Child Object with Field |
childLookup__c : Lookup(parentObjTrigger) field it is help to communicate on parent object. |
You can download file directly from github by click here
Final Output
Other post that would you like to learn
Step 1:- Apex Class Trigger : childObjTriggerRollUp.apxt
From Developer Console >> File >> New >> Apex Trigger
childObjTriggerRollUp.apxt [Apex Class Trigger]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
trigger childObjTriggerRollUp on childObjTrigger__c (before insert, before update, before delete, after insert, after update, after delete, after undelete) { List<childObjTrigger__c> childObjList = new List<childObjTrigger__c>(); Set<Id> accSetId = new Set<Id>(); if(trigger.isBefore){ system.debug('trigger before#'); }else if(trigger.isAfter){ system.debug('trigger after#'); if(trigger.isDelete){ childObjList = trigger.old; }else{ childObjList = trigger.new; } List<parentObjTrigger__c> updateParent = new List<parentObjTrigger__c>(); for(childObjTrigger__c childObjNewList:childObjList){ if(childObjNewList.childLookup__c != null){ accSetId.add(childObjNewList.childLookup__c); if(trigger.isUpdate){ childObjTrigger__c childObjOld = trigger.oldMap.get(childObjNewList.Id); system.debug('childObjOld ' + childObjOld); if(childObjOld.childLookup__c != childObjNewList.childLookup__c){ accSetId.add(childObjOld.childLookup__c); }else if(childObjOld.childLookup__c == null){ accSetId.add(childObjNewList.childLookup__c); } } } } //system.debug('accSetId ' + accSetId); for(parentObjTrigger__c parentSetObj: [Select Id, Name, childRollUp__c, (Select Id, childLookup__c From childObjTriggers__r ) From parentObjTrigger__c Where Id IN:accSetId]){ List<childObjTrigger__c> childSize = parentSetObj.childObjTriggers__r; parentSetObj.childRollUp__c=childSize.size(); updateParent.add(parentSetObj); } update updateParent; } } |
➡
TechW3web:- Apex Trigger to create rollup summary in Salesforce Example
Other related post that would you like to learn
great information
good post
You’re a beautiful inspiration. It really helps me in any situation. Where I stuck. Many of my friends told me to comment there post but I stuck what I should comment. Finally I got your post it always help me. Thanks for the lovely post.