Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger to update parent records field when child record is updated #1

Open
vijayk3327 opened this issue Aug 27, 2023 · 0 comments
Open
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@vijayk3327
Copy link
Owner

vijayk3327 commented Aug 27, 2023

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.

👉 To get source code live demo link, Click Here.

**Step 1:- Apex Class Trigger : checkboxStatus.apxt

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

`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;
        }   
    }   

}

}`

👉 To get source code live demo link, Click Here.

@vijayk3327 vijayk3327 added documentation Improvements or additions to documentation question Further information is requested labels Aug 27, 2023
@vijayk3327 vijayk3327 self-assigned this Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: No status
Development

No branches or pull requests

1 participant