Update Parent field when Child record is Updated or Inserted in Salesforce | Trigger updating parent with child value when a field on the child is changed

54 views

Hey guys, today in this post we are going to learn about Write Apex Trigger to update parent with child value when a field on the child is changed in Salesforce.

 

Final Output →

 

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!

 

 

Create Apex Trigger →

Step 1:- Create Apex Controller : checkboxStatus.apxt

SFDX:Create Apex Trigger>> New >> checkboxStatus.apxt

checkboxStatus.apxt [Apex Trigger]

  1.   TRIGGER checkboxStatus ON childObjTrigger__c (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
  2.  
  3.     IF(TRIGGER.isBefore){
  4.  
  5.     }ELSE IF(TRIGGER.isAfter){
  6.  
  7.         IF(TRIGGER.isInsert || TRIGGER.isUpdate){
  8.  
  9.            Map<Id,childObjTrigger__c> childMap = NEW Map<Id,childObjTrigger__c>();
  10.  
  11.             FOR(childObjTrigger__c c:TRIGGER.new){
  12.                 childMap.put(c.childLookup__c,c);
  13.             }
  14.  
  15.            List<parentObjTrigger__c> parentObjItem = NEW List<parentObjTrigger__c>();
  16.             List<parentObjTrigger__c> parntObj = [SELECT Id, Name, Status__c FROM parentObjTrigger__c WHERE Id IN:childMap.keySet()];
  17.  
  18.             FOR(parentObjTrigger__c p:parntObj){
  19.                 p.Status__c = childMap.get(p.Id).Status__c;
  20.                 parentObjItem.add(p);
  21.             }
  22.  
  23.             IF(!parentObjItem.isEmpty() && parentObjItem.size()>0){
  24.                 UPDATE parentObjItem;
  25.             }
  26.  
  27.  
  28.  
  29.         }
  30.  
  31.     }
  32.  
  33. }

 

 

Further post that would you like to learn in Salesforce

 

FAQ (Frequently Asked Questions)

Can we update parent record from child record in Salesforce?

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. Please find below screenshots for reference.

Can we update child to parent in Workflow?

You cannot update a child record's field from a parent object using a Workflow rule. Workflow rules are designed to operate on the record on which the rule is triggered and cannot directly update fields on related records.

Flow to Update Parent Record based on multiple Child Records

While looping through the lines, use an Assignment Element to add the current Quote Items Product Category to a text variable. For example, let's say the text variable is called varProductCategories.

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