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
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 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 116 117 118 119 120 121 122 |
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
- Your reaction of the article ▾
Great bro keep it up.
appriciate your hardwork
good post
Nice article .
your post nice
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.
Really very happy to say,your post is very interesting to read.I never stop myself to say something about it.You’re doing a great job.Keep it up
your page clear many doubts !
love problem solution
Very Nice Post
5 Internet Approval Site (Do Follow Backlinks)
Nice Article
Super and best articles and good blog and naccessary content.
your page clear many doubts !
Amazing article, and I have learnt a lot
It is a really nice post that I could learn so much information