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
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]
TRIGGER EmployeeSizeTrigger ON parentObjTrigger__c (BEFORE INSERT, BEFORE UPDATE, after INSERT, after UPDATE, BEFORE DELETE, after DELETE) {
Map<Id, List<childObjTrigger__c>> prntMap = NEW Map<Id, List<childObjTrigger__c>>();
Map<Id, parentObjTrigger__c> prntOldMap = NEW Map<Id, parentObjTrigger__c>();
Map<Id, Integer> prntMapChildNo = NEW Map<Id, Integer>();
Map<Id, Decimal> empValMap = NEW Map<Id, Decimal>();
Set<Id> parentKey = prntMap.keySet();
List<List<childObjTrigger__c>> childKeyVal = NEW List<List<childObjTrigger__c>>();
List<parentObjTrigger__c> oldVal = NEW List<parentObjTrigger__c>();
Set<Id> prntSetId = NEW Set<Id>();
List<childObjTrigger__c> childObjList = NEW List<childObjTrigger__c>();
List<childObjTrigger__c> childObjNull = NEW List<childObjTrigger__c>();
Set<Id> childNullSetId = NEW Set<Id>();
DECIMAL childSize;
DECIMAL empVal;
DECIMAL empValOld;
IF(TRIGGER.isBefore){
system.debug('Event before trigger');
}ELSE IF(TRIGGER.isAfter){
system.debug('Event after trigger');
oldVal = TRIGGER.old;
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()]){
prntMap.put(par.Id, par.childObjTriggers__r);
prntMapChildNo.put(par.Id, (par.childObjTriggers__r).size());
}
FOR(List<childObjTrigger__c> childObjVal:prntMap.values()){
childKeyVal.add(childObjVal);
}
List<parentObjTrigger__c> prntList = [SELECT Id, Name FROM parentObjTrigger__c WHERE Id IN:prntMap.keySet()];
FOR(parentObjTrigger__c pr1:prntList){
prntSetId.add(pr1.Id);
}
FOR(parentObjTrigger__c prantEmp:[SELECT Id,Employee__c FROM parentObjTrigger__c WHERE Id IN:prntMap.keySet()]){
empValMap.put(prantEmp.Id, prantEmp.Employee__c);
empVal = prantEmp.Employee__c;
}
List<childObjTrigger__c> childObjKey = NEW List<childObjTrigger__c>();
FOR(childObjTrigger__c childKey: [SELECT Id, Name,EmployeeName__c,childLookup__c FROM childObjTrigger__c WHERE childLookup__c=:prntSetId]){
childObjKey.add(childKey);
}
IF(TRIGGER.isInsert){
FOR(INTEGER i=0; i<empVal; i++){
FOR(parentObjTrigger__c prntSet:[SELECT Id, Name FROM parentObjTrigger__c WHERE Id IN:prntSetId]){
childObjTrigger__c childObj = NEW childObjTrigger__c();
childObj.EmployeeName__c='childMap ' + i;
childObj.childLookup__c=prntSet.id;
childObjList.add(childObj);
}
}
INSERT childObjList;
}
ELSE IF(TRIGGER.isUpdate){
FOR(parentObjTrigger__c parOld: oldVal){
empValOld = parOld.Employee__c;
}
IF(empVal !=empValOld){
FOR(INTEGER intNo:prntMapChildNo.values()){
childSize = intNo;
}
IF(empVal > childSize){
empVal = empVal - childSize;
FOR(INTEGER i=0; i<empVal; i++){
FOR(parentObjTrigger__c prntSet1:[SELECT Id, Name FROM parentObjTrigger__c WHERE Id IN:prntSetId]){
childObjTrigger__c childObj2 = NEW childObjTrigger__c();
childObj2.EmployeeName__c='update child ' + i;
childObj2.childLookup__c=prntSet1.Id;
childObjKey.add(childObj2);
}
}
upsert childObjKey;
}ELSE{
empVal = childSize - empVal;
FOR(INTEGER i=0; i<empVal; i++){
childObjKey[i].childLookup__c=NULL;
childObjNull.add(childObjKey[i]);
}
UPDATE childObjNull;
FOR(childObjTrigger__c childSet:childObjNull){
childNullSetId.add(childSet.Id);
}
List<childObjTrigger__c> deleteNull = [SELECT Id FROM childObjTrigger__c WHERE ID IN:childNullSetId];
DELETE deleteNull;
}
}
}
}
}
Further post that would you like to learn in Salesforce
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 |