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
Janno RipJanno Rip 

Update child account based on Update on parentid

Hello Devs,

I have the following use case: Whenever the custom field Update_child__c on a parent account is set to true, all child accounts should be set to true in this field as well.

This is what I got so far, but I am getting a "UpdateChildAccount: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: ()"

trigger UpdateChildAccount on Account (after update){
map<id,Account> acctId_to_acct = new map<id,Account>();
for(Account a : trigger.new){
if(trigger.oldmap.get(a.id).Update_child__ != a.Update_child__){
acctId_to_acct.put(a.id,a);
}
}
if(acctId_to_acct.size() > 0)
{
    List<Account> childAccounts = [select Update_child__,parentid from Account where id in :acctId_to_acct.keyset()];
    if(childAccounts <> NULL && childAccounts.size()>0)
    {
        for(Account a : childAccounts)
        {
            a.Update_child__ = acctId_to_acct.get(a.parentid).Update_child__;
           
        }
        update childAccounts;
    }
}   
}
Raj VakatiRaj Vakati
Make two changes one avoid recursive and SOQL query is from the parent record id 
Use the below code
 
public Class AvoidRecursion{
    private static boolean firstRun = true;
    public static boolean isFirstRun(){
    if(firstRun){
      firstRun = false;
      return true;
    }else{
        return firstRun;
    }
    }
}
 
trigger UpdateChildAccount on Account (after update){
	if(AvoidRecursion.isFirstRun())
    {
	map<id,Account> acctId_to_acct = new map<id,Account>();
		for(Account a : trigger.new){
			if(trigger.oldmap.get(a.id).Update_child__ != a.Update_child__){
				acctId_to_acct.put(a.id,a);
			}
		}
		if(acctId_to_acct.size() > 0)
		{
			List<Account> childAccounts = [select Update_child__,parentid from Account where Parentid in :acctId_to_acct.keyset()];
			if(childAccounts <> NULL && childAccounts.size()>0)
			{
				for(Account a : childAccounts)
				{
					If(acctId_to_acct.get(a.parentid)!=null){
						a.Update_child__ = acctId_to_acct.get(a.parentid).Update_child__c;
					}
				   
				}
				update childAccounts;
			}
		} 

	}		
}


 
SEKAR RAJ.SEKAR RAJ.
Hi Janno,

I think, there is no field name like Update_child__.
Please check the child object field name in the SOQL query.

trigger UpdateChildAccount on Account (after update){
map<id,Account> acctId_to_acct = new map<id,Account>();
for(Account a : trigger.new){
  if(trigger.oldmap.get(a.id).Update_child__c != a.Update_child__c){
       acctId_to_acct.put(a.id,a);
   }
}
if(acctId_to_acct.size() > 0)
{
// Query the child Object records based on the account id
List<Account> childAccounts = [select Update_child__c,parentid from Account where id in :acctId_to_acct.keyset()];
    if(childAccounts <> NULL && childAccounts.size()>0)
    {
        for(Account a : childAccounts)
        {
            a.Update_child__c = acctId_to_acct.get(a.parentid).Update_child__c;
           
        }
        update childAccounts;
    }
}   
}

Thanks,
SEKAR RAJ