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 |
Final Output
You can download file directly from github by Click Here.
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]
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
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 |