Hey guys, today in this post we are going to learn about How to update the parent record field based on child record trigger in Salesforce custom object
Real time scenarios:- Write a trigger on custom object where update parent records field whenever child record is inserted or updated.
Create two custom object, both are associated with lookup relationship with each other
1) Custom Object:- parentObjTrigger__c custom field:- Status__c (Checkbox boolean type)
2) Custom Object:- childObjTrigger__c custom field:- Status__c (Checkbox boolean type)
If status field of child object marked true then should be status field of parent object automatic marked as true.
Files we used in this post example:-
checkboxStatus.apxt | Apex Class Trigger | It is used for update parent object field based on child records |
Custom Parent Object:- parentObjTrigger__c Custom Field :- Status__c
|
Custom Parent Object with Custom Field | It is parent custom object used for building a lookup relationship with childObjTrigger__c |
Custom Child Object:- childObjTrigger__c Custom Field:- Status__c |
Custom Child Object with Custom Field | It is child custom object that is lookup relationship and associated with parentObjTrigger__c |
You can download file directly from github by click here
Final Output
Other post that would you like to learn
Step 1:- Apex Class Trigger : checkboxStatus.apxt
From Developer Console >> File >> New >> Apex Trigger
checkboxStatus.apxt [Apex Class Controller]
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 |
trigger checkboxStatus on childObjTrigger__c (before Insert, before update, after insert, after update, before delete, after delete) { if(trigger.isBefore){ //system.debug('I am inside before event'); } else if(trigger.isAfter){ //system.debug('I am inside after event'); if(trigger.isUpdate){ for(childObjTrigger__c myStatus: Trigger.new){ parentObjTrigger__c getParentObj = [SELECT Id, Status__c FROM parentObjTrigger__c WHERE Id = :myStatus.childLookup__c ]; childObjTrigger__c getChildObj = [SELECT Id, Status__c, childLookup__r.Status__c FROM childObjTrigger__c WHERE Id = :myStatus.Id]; getParentObj.Status__c= getChildObj.Status__c; update getParentObj; } } if(trigger.isInsert && (trigger.isAfter)){ for(childObjTrigger__c myStatus: Trigger.new){ parentObjTrigger__c getParentObj = [SELECT Id, Status__c FROM parentObjTrigger__c WHERE Id = :myStatus.childLookup__c ]; childObjTrigger__c getChildObj = [SELECT Id, Status__c, childLookup__r.Status__c FROM childObjTrigger__c WHERE Id = :myStatus.Id]; getParentObj.Status__c= getChildObj.Status__c; update getParentObj; } } if(Trigger.isDelete && (Trigger.isBefore)){ for(childObjTrigger__c myStatus: Trigger.old){ parentObjTrigger__c getParentObj = [SELECT Id, Status__c FROM parentObjTrigger__c WHERE Id = :myStatus.childLookup__c ]; childObjTrigger__c getChildObj = [SELECT Id, Status__c, childLookup__r.Status__c FROM childObjTrigger__c WHERE Id = :myStatus.Id]; delete getParentObj; } } } } |
Other related post that would you like to learn
That’s an interesting post. Check out below link for more interesting posts:
Pics Directory
good article
nicley explained
i like your blog it is very informative
In this blog I get a great post sir please also visit my blog I post great content
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.