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 related post that would you like to learn in Salesforce
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; } } } } |
Further post that would you like to learn in Salesforce
What will happen if we perform update on after update trigger?
You can call this method in the trigger, but they will be executed after trigger ends, so no error will happen. For example, I used it to automatically fill some fields that require ID of record, that can't be received in Before trigger.
What is difference between trigger new and trigger old?
The values in Trigger. old after the workflow update will NOT contain the “description” field that was updated in the workflow. The values in Trigger. new after the workflow update will contain any existing fields that were populated upon the object's creation AND the “description” workflow updated field.
What is the difference between before trigger and after trigger?
Before triggers are used to update or validate record values before they're saved to the database. After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to effect changes in other records.
Related Topics | You May Also Like
- Your reaction of the article ▾
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.
Nice Blog
Nice Content
This is Interesting Post
5 Internet Approval Site (Do Follow Backlinks)
this is the best article in this post so i am very impressed bcz this is very pretty
This is Interesting Post…
buy Google voice number
buy twitter accounts
Buy Instagram pva accounts
Buy Facebook pva accounts
Amazing article, and I have learnt a lot