function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anshuman ParhiAnshuman Parhi 

Write trigger to update status field of all related records.whenever there is change in status of record on either object(A,B).

Create Three Objects (A, B, C). Object 'C' will be the junction object. Create status field on object 'A' and 'B' (note:- Status:New Open Closed).
Write trigger to update status field of all related records.whenever there is change in status of record on either object(A,B).   

Any help for this....
Best Answer chosen by Anshuman Parhi
Suraj Tripathi 47Suraj Tripathi 47
Hi Anshuman,

Greetings!

Trigger on A Object
trigger TriggerOnA on A__c (after update) {
    Set<Id> AIdSet = new Set<Id>();
    for(A__c aObj : trigger.new) {
        if(aObj.Status__c != trigger.oldMap.get(aObj.Id).Status__c) {
            AIdSet.add(aObj.Id);
        }
    }
    if(!AIdSet.isEmpty()) {
        List<B__c> BList = new List<B__c>();
        for(C__c cObj : [SELECT Id, A__c, A__r.Status__c, B__c, B__r.Status__c FROM C__c WHERE A__c IN :AIdSet]) {
            if(cObj.A__r.Status__c != cObj.B__r.Status__c) {
                B__c bObj = new B__c();
                bObj.Id = cObj.B__c;
                bObj.Status__c = cObj.A__r.Status__c;
                BList.add(bObj);
            }
        }
        if(!BList.isEmpty()) {
            UPDATE BList;
        }
    }
}


Trigger on B Object
trigger TriggerOnB on B__c (after update) {
    Set<Id> BIdSet = new Set<Id>();
    for(B__c bObj : trigger.new) {
        if(bObj.Status__c != trigger.oldMap.get(bObj.Id).Status__c) {
            BIdSet.add(bObj.Id);
        }
    }
    if(!BIdSet.isEmpty()) {
        List<A__c> AList = new List<A__c>();
        for(C__c cObj : [SELECT Id, A__c, A__r.Status__c, B__c, B__r.Status__c FROM C__c WHERE B__c IN :BIdSet]) {
            if(cObj.B__r.Status__c != cObj.A__r.Status__c) {
                A__c aObj = new A__c();
                aObj.Id = cObj.A__c;
                aObj.Status__c = cObj.B__r.Status__c;
                AList.add(aObj);
            }
        }
        if(!AList.isEmpty()) {
            UPDATE AList;
        }
    }
}

If you find your Solution then mark this as the best answer. 

Thank you!
Regards,
Suraj Tripathi

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Anshuman,

Although there could be different ways to achieve it one way is to have triggers on both the objects and make a set of record id's that have change in status field value then send these set of values to a handler class that would be doing the necessary fetching of child records and updating the status on child records.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Anshuman ParhiAnshuman Parhi
Hi ANUTEJ

I wanted to k now trigger for this....so kindly give trigger code for this kind of scenario
Suraj Tripathi 47Suraj Tripathi 47
Hi Anshuman,

Greetings!

Trigger on A Object
trigger TriggerOnA on A__c (after update) {
    Set<Id> AIdSet = new Set<Id>();
    for(A__c aObj : trigger.new) {
        if(aObj.Status__c != trigger.oldMap.get(aObj.Id).Status__c) {
            AIdSet.add(aObj.Id);
        }
    }
    if(!AIdSet.isEmpty()) {
        List<B__c> BList = new List<B__c>();
        for(C__c cObj : [SELECT Id, A__c, A__r.Status__c, B__c, B__r.Status__c FROM C__c WHERE A__c IN :AIdSet]) {
            if(cObj.A__r.Status__c != cObj.B__r.Status__c) {
                B__c bObj = new B__c();
                bObj.Id = cObj.B__c;
                bObj.Status__c = cObj.A__r.Status__c;
                BList.add(bObj);
            }
        }
        if(!BList.isEmpty()) {
            UPDATE BList;
        }
    }
}


Trigger on B Object
trigger TriggerOnB on B__c (after update) {
    Set<Id> BIdSet = new Set<Id>();
    for(B__c bObj : trigger.new) {
        if(bObj.Status__c != trigger.oldMap.get(bObj.Id).Status__c) {
            BIdSet.add(bObj.Id);
        }
    }
    if(!BIdSet.isEmpty()) {
        List<A__c> AList = new List<A__c>();
        for(C__c cObj : [SELECT Id, A__c, A__r.Status__c, B__c, B__r.Status__c FROM C__c WHERE B__c IN :BIdSet]) {
            if(cObj.B__r.Status__c != cObj.A__r.Status__c) {
                A__c aObj = new A__c();
                aObj.Id = cObj.A__c;
                aObj.Status__c = cObj.B__r.Status__c;
                AList.add(aObj);
            }
        }
        if(!AList.isEmpty()) {
            UPDATE AList;
        }
    }
}

If you find your Solution then mark this as the best answer. 

Thank you!
Regards,
Suraj Tripathi
This was selected as the best answer