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
Rachel Linder 8Rachel Linder 8 

Writing Trigger to Update Child Records based on a field on the Parent Record

I have started writing the following trigger to update child records based on a checkbox field being updated on the Parent Record:

trigger Agency_in_Collections on Account (after update) {
 
   for (Account Agency: Trigger.new){
        Account oldAgency = Trigger.oldMap.get(Agency.Id);
           if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
              {
              List<Account> children = [ SELECT Id, AccountID, Agency_in_Collections__c from
                Account where AccountID = :Agency.ID];
                  List<Account> newids = new List <Account>
                  ();
                    for(Account acc: children){
                       if(acc.Agency_in_Collections__c != acc.Agency_in_Collections__c){
                           acc.Agency_in_Collections__c = acc.Agency_in_Collections__c;
                               newids.add(acc);
                               }
                            }
                            if (newids.isEmpty()== false){
                                update newids;
                            }
                     }
          }
 }


The checkbox field on the Parent Record is called "Agency in Collections". The field name on the Child Record is "Agency on Collections". 

I was working on this in sandbox and was getting an error when saving the trigger. The error is: 

User-added image
Please help. If there is a better way as well please let me tknow.
 
Raj VakatiRaj Vakati
Try this code
 
trigger Agency_in_Collections on Account (after update) {
 
   for (Account Agency: Trigger.new){
        Account oldAgency = Trigger.oldMap.get(Agency.Id);
           if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
              {
				  
              List<Account> children = [ SELECT Id, AccountID, Agency_in_Collections__c from
                Account where Parent.id  = :Agency.ID];
				
                  List<Account> newids = new List <Account>();
                       for(Account acc: children){
                           acc.Agency_in_Collections__c = Agency.Agency_in_Collections__c;
                               newids.add(acc);
                        }
                      if (newids.isEmpty()== false){
                                update newids;
                          }
                     }
          }
 }

 
Maharajan CMaharajan C
Hi Rachel,

Don't use the Soql , DML inside the for Loop it will hit the governor asap once u processing the bulk data processing:

I hope you are using the Account Hierarchy field here to make the relation between two Accounts if it's yes then the ParentId field below or you are using any custom relationship field then use that field api name below

Please try the below code:

trigger Agency_in_Collections on Account (after update) {
    
   set<Id> ParentIds = new set<Id>();
   List<Account> children = new List<Account>();
   List<Account> accountstoUpdate = new List<Account>();

   Map<Id,String> accountMap = new Map<Id,String>();
   for (Account Agency: Trigger.new){
        Account oldAgency = Trigger.oldMap.get(Agency.Id);
           if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
              {
                ParentIds.add(Agency.Id);
                accountMap.put(Agency.Id,String.ValueOf(Agency_in_Collections__c)); ////If the  Agency_in_Collections__c is lookup then use Map<Id,Id> don't use string.
              }
            
          }
          if(ParentIds.size() > 0)
            {
                children = [ SELECT Id, AccountID, Agency_in_Collections__c from
                Account where ParentId = :Agency.ID];     ///// If parentId is not working then use the Parent.Id
            }
            
        if(children.size() > 0)
        {
        for(Account acc:children )
        {
            if(accountMap.containsKey(acc.ParentId))
            {
            acc.Agency_in_Collections__c = accountMap.get(acc.ParentId);
            }
            accountstoUpdate.add(acc);
        }            
        }    

if(accountstoUpdate.size() > 0)
{
update accountstoUpdate;
}        
 }

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
Rachel Linder 8Rachel Linder 8
Hi Team, I just want to give a little more information. So, we have Account Record types. So here is what we are trying to do/have happen:

On the Local Agency Account Record Type when the checkbox field called Agency in Collections is checked then we want to update the checkbox field Agency in Collections on the Advertiser Child Record Type.  We have accounts as a related list to accounts. So, our "hierarchy" starts with a record type of Local Agency. Then we create "Advertiser" record types underneath.

Hope that helps.
Rachel Linder 8Rachel Linder 8
@Maharajan - i used your code above but I am getting the error below.User-added image
Maharajan CMaharajan C
Try the below code: 

trigger Agency_in_Collections on Account (after update) {
    
   set<Id> ParentIds = new set<Id>();
   List<Account> children = new List<Account>();
   List<Account> accountstoUpdate = new List<Account>();

   Map<Id,boolean> accountMap = new Map<Id,boolean>();
   for (Account Agency: Trigger.new){
        Account oldAgency = Trigger.oldMap.get(Agency.Id);
           if(oldAgency.Agency_in_Collections__c != Agency.Agency_in_Collections__c && Agency.Agency_in_Collections__c == true)
              {
                ParentIds.add(Agency.Id);
                accountMap.put(Agency.Id,Agency.Agency_in_Collections__c);
              }
            
          }
          if(ParentIds.size() > 0)
            {
                children = [ SELECT Id, AccountID, Agency_in_Collections__c from
                Account where ParentId IN: ParentIds];    
            }
            
        if(children.size() > 0)
        {
        for(Account acc:children )
        {
            if(accountMap.containsKey(acc.ParentId))
            {
            acc.Agency_in_Collections__c = accountMap.get(acc.ParentId);
            }
            accountstoUpdate.add(acc);
        }            
        }    

if(accountstoUpdate.size() > 0)
{
update accountstoUpdate;
}        
 }
Rachel Linder 8Rachel Linder 8
I tried this code and I am getting the following error now: Again this is a field on the Account Object using two different Record Types. And the relationship between the Parent and Child Object is that the child Object is a related Account List to the Parent Account.

User-added image

Here is a quick screenshot of the Local Agency Page Layout showing the related list to the child records.
User-added image
Rachel Linder 8Rachel Linder 8
Hello there, just following up for any additional input.
Rachel Linder 8Rachel Linder 8
I was able to get what I needed via process builder.