Hey guys, today in this post we are going to learn about Apex trigger to create and count the number of child records associated with parent object based on custom field when parent record is created/updated 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 value of employee size, rest records of child object should be automatic removed.
Files we used in this post example
EmployeeSizeTrigger2.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. |
Live Example
Other related post that would you like to learn.
Step 1:- Apex Class Trigger : EmployeeSizeTrigger2.apxt
From Developer Console >> File >> New >> Apex Trigger
EmployeeSizeTrigger2.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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
trigger EmployeeSizeTrigger2 on parentObjTrigger__c (before insert, before update, after insert, after update, before delete, after delete) { List<parentObjTrigger__c> prntList= new List<parentObjTrigger__c>(); Set<Id> setOldId= new Set<Id>(); List<childObjTrigger__c> childListNew= new List<childObjTrigger__c>(); List<childObjTrigger__c> childList= new List<childObjTrigger__c>(); Set<Id> childSetId = new Set<Id>(); if(trigger.isBefore){ system.debug('Event before trigger'); if(trigger.isDelete){ prntList=trigger.old; prntList = [Select Id, Name, Status__c, LastModifiedDate__c, (Select Id, Status__c, LastModifiedDate From childObjTriggers__r) From parentObjTrigger__c Where Id=:trigger.old]; system.debug('prntListOld' + prntList); for(parentObjTrigger__c oldList: prntList){ for(childObjTrigger__c childList: oldList.childObjTriggers__r){ system.debug('childList ' + childList); setOldId.add(childList.Id); } } system.debug('setOldId## ' + setOldId ); List<childObjTrigger__c> childId=[Select Id, Name From childObjTrigger__c Where Id IN:setOldId]; delete childId; } }else if(trigger.isAfter){ system.debug('Event after trigger'); decimal empVal; if(trigger.isDelete){ system.debug('setOldId## ' + setOldId ); } else if(trigger.isInsert){ prntList=trigger.new; for(parentObjTrigger__c prntNew:prntList){ empVal = prntNew.Employee__c; } for(Integer i=0; i<empVal; i++){ for(parentObjTrigger__c newRow:prntList){ childObjTrigger__c childItem = new childObjTrigger__c(); childItem.EmployeeName__c='Employee Name ' + i; childItem.childLookup__c= newRow.Id; childList.add(childItem); } } insert childList; } else if(trigger.isUpdate){ prntList=trigger.new; system.debug('trigger is updated'); List<childObjTrigger__c> childObjRelList = [Select Id, Status__c, LastModifiedDate From childObjTrigger__c Where childLookup__c=:prntList[0].Id]; for(parentObjTrigger__c prntNew:prntList){ empVal = prntNew.Employee__c; } for(parentObjTrigger__c updatePtr:prntList){ for(childObjTrigger__c childItemRel:childObjRelList){ system.debug('childItemRel ' + childItemRel); childSetId.add(childItemRel.Id); } } system.debug('empVal Before ' + empVal); if(empVal > childSetId.size()){ empVal = empVal-childSetId.size(); system.debug('empVal After ' + empVal); for(Integer i=0; i<empVal; i++){ system.debug('empValAA ' + empVal + i); for(parentObjTrigger__c updatePtr2:prntList){ childObjTrigger__c childItem2 = new childObjTrigger__c(); childItem2.EmployeeName__c='childSetEmp ' + i; childItem2.childLookup__c= updatePtr2.Id; childListNew.add(childItem2); } } system.debug('childListNew 2# ' + childListNew ); upsert childListNew; }else{ Set<Id> resId = new Set<Id>(); system.debug('empVal$$ ' + empVal); List<childObjTrigger__c> childObj = new List<childObjTrigger__c>(); List<childObjTrigger__c> childObjNullVal = new List<childObjTrigger__c>(); for(parentObjTrigger__c parentRelList:prntList){ for(childObjTrigger__c childItemRel:childObjRelList){ system.debug('childItemRel1 ' + childItemRel); resId.add(childItemRel.Id); childObj.add(childItemRel); } } empVal = resId.size() - empVal; system.debug('empVal New val ' + empVal); for(Integer i=0; i<empVal; i++){ system.debug('empVal Loop Val ' + i); childObj[i].childLookup__c=null; childObjNullVal.add(childObj[i]); } update childObjNullVal; system.debug('childObjNullVal ' + childObjNullVal); system.debug('childObj # ' + childObj); system.debug('resId #1 ' + resId); system.debug('childObj$ ' + childObj); } } } } |
➡
TechW3web:- Apex Trigger to Update Count of Child Records into Parent Object Custom Field in Salesforce
Further post that would you like to learn in LWC
A thankful and good information by you thanks for this info.
helpful info share…. great