Trigger to update count of child records with custom field of parent object | count of child records on parent for lookup relationship uses of apex trigger in Salesforce

1,730 views

Hey guys, today in this post we are going to learn about how to update count of child records with custom field value into parent object using trigger map list in Salesforce.

Real time scenarios:- Write a trigger on parent object where create a custom field as Employee (Number type).

If user insert the parent record, the child record should be automatic inserted equal to the same value of employee.

Or if user update the value of employee less then the total number of child records, in this case the child records should be exist only equal to employee size, rest records of child object should be automatic removed.

parentObjTrigger:- Custom Fields & Relationships

Create a custom field on parent Object name as Employee__c (Number Type).

childObjTrigger:- Custom Fields & Relationships

Create a lookup relationship field on child Object name as childLookup__c :: Lookup(parentObjTrigger).

Files we used in this post example:-

EmployeeSizeTrigger.apxt Apex Class Trigger It will be fired wheneverΒ insert or update a record on parent object

Parent Cusotm Object:- parentObjTrigger__c

Custom Field:- EmployeeName__c

Parent Object with Custom Field Trigger on parent object and updae the count of related child records with theΒ custom field value of parent object.

Child Custom Object:- childObjTrigger__c

Custom Field:-Β childLookup__cΒ : Lookup(parentObjTrigger)

Child Object with Custom Field childLookup__cΒ : Lookup(parentObjTrigger) field it is help to communicate on parent object.

Final Output

Trigger to update parent object value -- w3web.net

 

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 : EmployeeSizeTrigger.apxt

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

EmployeeSizeTrigger.apxt [Apex Class Trigger]

  1.   TRIGGER EmployeeSizeTrigger ON parentObjTrigger__c (BEFORE INSERT, BEFORE UPDATE, after INSERT, after UPDATE, BEFORE DELETE, after DELETE) {
  2.     Map<Id, List<childObjTrigger__c>> prntMap = NEW Map<Id, List<childObjTrigger__c>>();
  3.     Map<Id, parentObjTrigger__c> prntOldMap = NEW Map<Id, parentObjTrigger__c>();
  4.     Map<Id, Integer> prntMapChildNo = NEW Map<Id, Integer>(); 
  5.     Map<Id, Decimal> empValMap = NEW Map<Id, Decimal>();  
  6.     Set<Id> parentKey = prntMap.keySet();
  7.     List<List<childObjTrigger__c>> childKeyVal = NEW List<List<childObjTrigger__c>>();
  8.     List<parentObjTrigger__c> oldVal = NEW List<parentObjTrigger__c>();    
  9.     Set<Id> prntSetId = NEW Set<Id>();
  10.  
  11.     List<childObjTrigger__c> childObjList = NEW List<childObjTrigger__c>();
  12.     List<childObjTrigger__c> childObjNull = NEW List<childObjTrigger__c>();
  13.     Set<Id> childNullSetId = NEW Set<Id>();
  14.  
  15.     DECIMAL childSize;
  16.     DECIMAL empVal;
  17.     DECIMAL empValOld;
  18.     IF(TRIGGER.isBefore){
  19.         system.debug('Event before trigger');
  20.  
  21.     }ELSE IF(TRIGGER.isAfter){
  22.         system.debug('Event after trigger');
  23.         oldVal = TRIGGER.old;
  24.  
  25.  
  26.         FOR(parentObjTrigger__c par: [SELECT Id, Name, Employee__c, (SELECT Id, childLookup__c FROM childObjTriggers__r) FROM parentObjTrigger__c WHERE Id IN: TRIGGER.newMap.values()]){           
  27.             prntMap.put(par.Id, par.childObjTriggers__r);
  28.             prntMapChildNo.put(par.Id, (par.childObjTriggers__r).size());
  29.  
  30.         }           
  31.  
  32.  
  33.         FOR(List<childObjTrigger__c> childObjVal:prntMap.values()){
  34.  
  35.             childKeyVal.add(childObjVal);       
  36.         }
  37.  
  38.  
  39.         List<parentObjTrigger__c> prntList = [SELECT Id, Name FROM parentObjTrigger__c WHERE Id IN:prntMap.keySet()];
  40.         FOR(parentObjTrigger__c pr1:prntList){
  41.             prntSetId.add(pr1.Id);
  42.         }
  43.  
  44.         FOR(parentObjTrigger__c prantEmp:[SELECT Id,Employee__c FROM parentObjTrigger__c WHERE Id IN:prntMap.keySet()]){
  45.             empValMap.put(prantEmp.Id, prantEmp.Employee__c);
  46.             empVal = prantEmp.Employee__c;
  47.  
  48.  
  49.         }
  50.  
  51.  
  52.         List<childObjTrigger__c> childObjKey = NEW List<childObjTrigger__c>();
  53.         FOR(childObjTrigger__c childKey: [SELECT Id, Name,EmployeeName__c,childLookup__c FROM childObjTrigger__c WHERE childLookup__c=:prntSetId]){
  54.  
  55.             childObjKey.add(childKey);
  56.  
  57.         }   
  58.  
  59.             IF(TRIGGER.isInsert){
  60.                 FOR(INTEGER i=0; i<empVal; i++){
  61.                     FOR(parentObjTrigger__c prntSet:[SELECT Id, Name FROM parentObjTrigger__c WHERE Id IN:prntSetId]){
  62.                         childObjTrigger__c childObj = NEW childObjTrigger__c();
  63.                         childObj.EmployeeName__c='childMap ' + i;
  64.                         childObj.childLookup__c=prntSet.id;
  65.                         childObjList.add(childObj);
  66.                     } 
  67.  
  68.                 } 
  69.  
  70.                 INSERT childObjList;
  71.  
  72.             } 
  73.             ELSE IF(TRIGGER.isUpdate){  
  74.               FOR(parentObjTrigger__c parOld: oldVal){
  75.                     empValOld = parOld.Employee__c;
  76.                 }
  77.  
  78.                IF(empVal !=empValOld){ 
  79.                 FOR(INTEGER intNo:prntMapChildNo.values()){
  80.  
  81.                     childSize = intNo;
  82.                 }
  83.  
  84.                 IF(empVal > childSize){
  85.                     empVal = empVal - childSize;
  86.                     FOR(INTEGER i=0; i<empVal; i++){
  87.                         FOR(parentObjTrigger__c prntSet1:[SELECT Id, Name FROM parentObjTrigger__c WHERE Id IN:prntSetId]){  
  88.                             childObjTrigger__c childObj2 = NEW childObjTrigger__c();
  89.                             childObj2.EmployeeName__c='update child ' + i;
  90.                             childObj2.childLookup__c=prntSet1.Id;
  91.                             childObjKey.add(childObj2); 
  92.                         }   
  93.                     }
  94.  
  95.                     upsert childObjKey;
  96.  
  97.                 }ELSE{
  98.                     empVal = childSize - empVal;
  99.  
  100.  
  101.                     FOR(INTEGER i=0; i<empVal; i++){
  102.                         childObjKey[i].childLookup__c=NULL;
  103.                         childObjNull.add(childObjKey[i]);
  104.                     }
  105.  
  106.                     UPDATE childObjNull;
  107.  
  108.                     FOR(childObjTrigger__c childSet:childObjNull){
  109.                         childNullSetId.add(childSet.Id);
  110.                     }
  111.  
  112.                     List<childObjTrigger__c> deleteNull = [SELECT Id FROM childObjTrigger__c WHERE ID IN:childNullSetId];
  113.                     DELETE deleteNull;
  114.                 }
  115.  
  116.                }  
  117.  
  118.             }            
  119.  
  120.     }   
  121.  
  122. }

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

Can we update parent to child in process builder?

You can use process builder to update parent record from child. Create a process builder on child object and update parent object, here I am using account and contact. Created process builder on contact and updating account status field when contact status is rejected.

Can we update child record using workflow?

Salesforce allows updating of the parent fields through a workflow on child object.

Can we update parent record using workflow?

For updating related records, Process Builder can update any field on any related record, where Workflow can only update some fields on a parent record of a Master-Detail relationship. Process Builder can also update multiple related records in a situation when all of a record's child records need the same update.

Related Topics | You May Also Like

 
  

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