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
LisaSturzLisaSturz 

Trigger to update a field on all child objects when a parent object field is updated

I have a junction object created between Accounts and Contacts called Affiliations. Any time a user updates a particular field on an Account record(ACO CMS Pending checkbox = false), I need to update a field on all of the related Affiliation records (ACO Participation = Yes). Could anyone point me in the right direction to getting this working? I tried a cross-object workflow rule, but it didn't appear to be working because I need to update the child records and not the parent record, so I am assuming I need to write a trigger. Unfortunately, this is my first so I could use assistance. Thanks!

Dhaval PanchalDhaval Panchal

Try this,

 

//--Trigger--
trigger trgUpdateChild on Account (after update) {
    Set<ID> setAccountIds = new Set<ID>();
    //Create a list of new updated accounts.
    for(Account newAcc:Trigger.New){
        if(newAcc.ACO_CMS_Pending__c == false && Trigger.oldMap.get(newAcc.Id).ACO_CMS_Pending__c != newAcc.ACO_CMS_Pending__c){
            setAccountIds.add(newAcc.Id);
        }
    }
    
    //Fetch reference records from Affiliations
    List<Affiliations__c> lstAffiliations = new List<Affiliations__c>();
    Set<ID> setContactIds = new Set<ID>();
    Map<ID, Account> mapAccount = new Map<ID, Account>();
    if(setAccountIds.size()>0){
        //I have considered that api name of lookup fields for account and contact is Account__c and Contact__C
        lstAffiliations = [Select id, Account__c, Contact__c from Affiliations__c Where Account__c in:setAccountIds];
        if(lstAffiliations.size()>0){
            for(Affiliations__c aff:lstAffiliations){
                setContactIds.add(aff.Contact__c);
                mapAccount(aff.Contact__c, Trigger.newMap.get(aff.Account__c));
            }
        }
    }
    
    //Fetch contacts
    List<Contact> lstContact = new List<Contact>();
    if(setContactIds.size()>0){
        lstContact = [Select id, ACO_Participation__c From Contact Where id in:setContactIds];
    }
    //List of contacts which needs to be updated
    List<Contact> lstUpdateContact = new List<Contact>();
    for(Contact con:lstContact){
        if(mapAccount.size()>0 && mapAccount.containsKey(con.Id)){
            Account acc = mapAccount.get(con.Id);
            //Hear you have account object and contact object, you can do your logic here
            //based on account values etc..
            //for example see below.
            con.ACO_Participation__c = 'Yes';
            lstUpdateContact.add(con);
            //If logic is fixed (when account.ACO_CMS_Pending__c == false then contact.ACO_Participation__c = 'Yes') then you don need
            //to create Map
        }
    }
    if(lstUpdateContact.size()>0) update lstUpdateContact;
}
LisaSturzLisaSturz
Thank you so much! I am very appreciative! I created the trigger and updated our custom field names, but I am getting the following error:
Error: Compile Error: Method does not exist or incorrect signature: mapAccount(Id, SOBJECT:Account) at line 21 column 17

Just to clarify, it is always fixed, when ACO_CMS_Pending__c == false then ACO_Participating__c = 'Yes' ; however the ACO_Participating__c field is on the Affiliation junction object not on the Contact. I am so very appreciative of your assistance, as I am very new to this and a bit lost. Lisa

//--Trigger--
trigger trgUpdateChild on Account (after update) {
Set setAccountIds = new Set();
//Create a list of new updated accounts.
for(Account newAcc:Trigger.New){
if(newAcc.ACO_CMS_Pending__c == false && Trigger.oldMap.get(newAcc.Id).ACO_CMS_Pending__c != newAcc.ACO_CMS_Pending__c){
setAccountIds.add(newAcc.Id);
}
}

//Fetch reference records from Affiliation
List lstAffiliation = new List();
Set setContactIds = new Set();
Map mapAccount = new Map();
if(setAccountIds.size()>0){
//I have considered that api name of lookup fields for account and contact is Facility__c and Provider__C
lstAffiliation = [Select id, Facility__c, Provider__c from Affiliation__c Where Facility__c in:setAccountIds];
if(lstAffiliation.size()>0){
for(Affiliation__c aff:lstAffiliation){
setContactIds.add(aff.Provider__c);
mapAccount(aff.Provider__c, Trigger.newMap.get(aff.Facility__c));
}
}
}

//Fetch contacts
List lstContact = new List();
if(setContactIds.size()>0){
lstContact = [Select id, ACO_Participating__c From Affiliation__c Where id in:setContactIds];
}
//List of contacts which needs to be updated
List lstUpdateContact = new List();
for(Contact con:lstContact){
if(mapAccount.size()>0 && mapAccount.containsKey(con.Id)){
Account acc = mapAccount.get(con.Id);
//Hear you have account object and contact object, you can do your logic here
//based on account values etc..
//for example see below.
con.ACO_Participating__c = 'Yes';
lstUpdateContact.add(con);
//If logic is fixed (when account.ACO_CMS_Pending__c == false then contact.ACO_Participating__c = 'Yes') then you don need
//to create Map
}
}
if(lstUpdateContact.size()>0) update lstUpdateContact;
}


Lisa Sturycz
IT Business Analyst, EHR Ambulatory Team
Dignity Health
3033 North 3rd Avenue | Phoenix, AZ 85013
715-304-9394 (Cell) | 602-217-1725 (Office)
Lisa.Sturycz@DignityHealth.org
[cid:image001.png@01CE9FEC.382D8A90]
Caution: This email message, including all content and attachments, is both proprietary and CONFIDENTIAL. The information contained in this email message is intended only for the use of the recipient(s) named above. If the reader of this message is not the intended recipient or an agent responsible for delivering it to the intended recipient, you have received this document in error. Any further review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify us immediately by reply email and delete it. Thank you.
Kelly KingKelly King
Good afternoon Lisa,

Were you ever able to get this trigger working properly? If so, can you share how you accomplished this? Thank you!