Trigger to update parent records field when child record is updated | update parent field from child using apex trigger

5,025 views


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

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

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

Final Output

w3web.net -- update parent records field from child object

 

You can download file directly from github by Click Here.

 

Other related post that would you like to learn in Salesforce

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

Step 1:- Apex Class Trigger : checkboxStatus.apxt

From Developer Console >> File >> New >> Apex Trigger

checkboxStatus.apxt [Apex Class Controller]

  1.   TRIGGER checkboxStatus ON childObjTrigger__c (BEFORE INSERT, BEFORE UPDATE, after INSERT, after UPDATE, BEFORE DELETE, after DELETE) {
  2.     IF(TRIGGER.isBefore){
  3.         //system.debug('I am inside before event');   
  4.     }
  5.     ELSE IF(TRIGGER.isAfter){
  6.         //system.debug('I am inside after event');
  7.  
  8.         IF(TRIGGER.isUpdate){        
  9.             FOR(childObjTrigger__c myStatus: TRIGGER.new){      
  10.                 parentObjTrigger__c getParentObj = [SELECT Id, Status__c FROM parentObjTrigger__c WHERE Id = :myStatus.childLookup__c ];         
  11.                 childObjTrigger__c getChildObj = [SELECT Id, Status__c, childLookup__r.Status__c FROM childObjTrigger__c WHERE Id = :myStatus.Id];                  
  12.                 getParentObj.Status__c= getChildObj.Status__c;
  13.                 UPDATE getParentObj;         
  14.             }
  15.  
  16.         }
  17.  
  18.  
  19.         IF(TRIGGER.isInsert && (TRIGGER.isAfter)){
  20.             FOR(childObjTrigger__c myStatus: TRIGGER.new){
  21.                 parentObjTrigger__c getParentObj = [SELECT Id, Status__c FROM parentObjTrigger__c WHERE Id = :myStatus.childLookup__c ];         
  22.                 childObjTrigger__c getChildObj = [SELECT Id, Status__c, childLookup__r.Status__c FROM childObjTrigger__c WHERE Id = :myStatus.Id];
  23.                 getParentObj.Status__c= getChildObj.Status__c;        
  24.                 UPDATE getParentObj;
  25.             }   
  26.         }
  27.  
  28.  
  29.         IF(TRIGGER.isDelete && (TRIGGER.isBefore)){
  30.             FOR(childObjTrigger__c myStatus: TRIGGER.old){          
  31.                 parentObjTrigger__c getParentObj = [SELECT Id, Status__c FROM parentObjTrigger__c WHERE Id = :myStatus.childLookup__c ];         
  32.                 childObjTrigger__c getChildObj = [SELECT Id, Status__c, childLookup__r.Status__c FROM childObjTrigger__c WHERE Id = :myStatus.Id];                           
  33.                 DELETE getParentObj;
  34.             }   
  35.         }   
  36.  
  37.     }
  38.  
  39.  
  40. }

 

Further post that would you like to learn in Salesforce

 

 

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment