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
Create Apex Trigger →
Step 1:- Create Apex Controller : checkboxStatus.apxt
SFDX:Create Apex Trigger>> New >> checkboxStatus.apxt
checkboxStatus.apxt [Apex Trigger]
TRIGGER checkboxStatus ON childObjTrigger__c (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
IF(TRIGGER.isBefore){
}ELSE IF(TRIGGER.isAfter){
IF(TRIGGER.isInsert || TRIGGER.isUpdate){
Map<Id,childObjTrigger__c> childMap = NEW Map<Id,childObjTrigger__c>();
FOR(childObjTrigger__c c:TRIGGER.new){
childMap.put(c.childLookup__c,c);
}
List<parentObjTrigger__c> parentObjItem = NEW List<parentObjTrigger__c>();
List<parentObjTrigger__c> parntObj = [SELECT Id, Name, Status__c FROM parentObjTrigger__c WHERE Id IN:childMap.keySet()];
FOR(parentObjTrigger__c p:parntObj){
p.Status__c = childMap.get(p.Id).Status__c;
parentObjItem.add(p);
}
IF(!parentObjItem.isEmpty() && parentObjItem.size()>0){
UPDATE parentObjItem;
}
}
}
}
Further post that would you like to learn in Salesforce
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
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 |