Create rollup summary using Apex trigger on custom object | rollup summary trigger for count of child records on custom object in Salesforce example

4,487 views

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).

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

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

create rollup summary using Apex trigger -- w3web.net

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

Step 1:- Apex Class Trigger : childObjTriggerRollUp.apxt

From Developer Console >> File >> New >> Apex Trigger

childObjTriggerRollUp.apxt [Apex Class Trigger]

  1.   TRIGGER childObjTriggerRollUp ON childObjTrigger__c (BEFORE INSERT, BEFORE UPDATE, BEFORE DELETE, after INSERT, after UPDATE, after DELETE, after undelete) {
  2.     List<childObjTrigger__c> childObjList = NEW List<childObjTrigger__c>();
  3.    Set<Id> accSetId = NEW Set<Id>();  
  4.     IF(TRIGGER.isBefore){
  5.         system.debug('trigger before#');
  6.     }ELSE IF(TRIGGER.isAfter){
  7.        system.debug('trigger after#');
  8.         IF(TRIGGER.isDelete){
  9.            childObjList = TRIGGER.old;            
  10.         }ELSE{
  11.             childObjList = TRIGGER.new;
  12.         }
  13.  
  14.  
  15.  
  16.         List<parentObjTrigger__c> updateParent = NEW List<parentObjTrigger__c>();
  17.  
  18.         FOR(childObjTrigger__c childObjNewList:childObjList){
  19.             IF(childObjNewList.childLookup__c != NULL){
  20.                 accSetId.add(childObjNewList.childLookup__c);
  21.  
  22.                 IF(TRIGGER.isUpdate){
  23.                    childObjTrigger__c childObjOld = TRIGGER.oldMap.get(childObjNewList.Id);
  24.  
  25.                     system.debug('childObjOld ' + childObjOld);
  26.                     IF(childObjOld.childLookup__c != childObjNewList.childLookup__c){
  27.                         accSetId.add(childObjOld.childLookup__c);
  28.                     }ELSE IF(childObjOld.childLookup__c == NULL){
  29.                         accSetId.add(childObjNewList.childLookup__c);
  30.                     }
  31.                  }
  32.  
  33.             }           
  34.         }
  35.  
  36.         //system.debug('accSetId ' + accSetId); 
  37.         FOR(parentObjTrigger__c parentSetObj: [SELECT Id, Name, childRollUp__c, (SELECT Id, childLookup__c FROM childObjTriggers__r ) FROM parentObjTrigger__c WHERE Id IN:accSetId]){
  38.             List<childObjTrigger__c> childSize = parentSetObj.childObjTriggers__r;
  39.             parentSetObj.childRollUp__c=childSize.size();
  40.             updateParent.add(parentSetObj);
  41.         }
  42.         UPDATE updateParent;
  43.  
  44.     }
  45.  
  46. }

 

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

Does rollup summary field fire trigger?

Yes, Changes in rollup summary field can cause trigger to fire. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.

Can we create roll up summary fields in lookup relationship?

Unfortunately, roll-up summary fields are only available for objects in a Master-Detail relationship and are not available for those that have a Lookup relationship.

How many roll-up summary fields can be created in an object?

In general, first try to set up your reports to make these calculations before requesting a limit increase. Note: While increases can be submitted to Support, the maximum hard-coded limit for roll-up summary fields is 40 per object and cannot be increased above that.

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment