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
DaNae PetersonDaNae Peterson 

Update child accounts based off changes made on the parent

In my org we utilitze account hierarchy with multiple child accounts to one parent account.  I need a way to update all the child accounts when any one of 6 certian fields is changed on the parent (owner, status, and 4 custom fields (2 checkboxes, 2 look up fields)).  I know the best way to approach this is through a trigger but my experience with this is extremely limited... 

Can someone lend a guiding hand?  It sounds like I need to use trigger.oldMap but not sure how to start.  Even just helping me to set it up would be most helpful...  Thank you!
Avijit Chakraborty 11Avijit Chakraborty 11
What exactly you are looking for code ? 
DaNae PetersonDaNae Peterson
@[Avijit Chakraborty 11] I need to be able to update all child accounts when any of 6 fields change on the parent account.  This is the code I have come up with:

(When I tested it out, the following error occurred: Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateChildAccount caused an unexpected exception, contact your administrator: UpdateChildAccount: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.ParentId: Trigger.UpdateChildAccount: line 11, column 1)


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).OwnerId != a.OwnerId || trigger.oldmap.get(a.id).salesReach__Agent_Account_Company_Name__c != a.salesReach__Agent_Account_Company_Name__c || trigger.oldmap.get(a.id).salesReach__Agent_Contact_Name__c != a.salesReach__Agent_Contact_Name__c || trigger.oldmap.get(a.id).Diamond_Account__c != a.Diamond_Account__c || trigger.oldmap.get(a.id).Net_One__c != a.Net_One__c || trigger.oldmap.get(a.id).Account_Status__c != a.Account_Status__c){
acctId_to_acct.put(a.id,a);
}
}
if(acctId_to_acct.size() > 0){
List<Account> childAccounts = [select OwnerId, salesReach__Agent_Account_Company_Name__c, salesReach__Agent_Contact_Name__c, Diamond_Account__c, Net_One__c, Account_Status__c from Account where id in :acctId_to_acct.keyset()];
for(Account a : childAccounts){
a.Ownerid = acctId_to_acct.get(a.parentid).Ownerid;
a.salesReach__Agent_Account_Company_Name__c = acctId_to_acct.get(a.parentid).salesReach__Agent_Account_Company_Name__c;
a.salesReach__Agent_Contact_Name__c = acctId_to_acct.get(a.parentid).salesReach__Agent_Contact_Name__c;
a.Diamond_Account__c = acctId_to_acct.get(a.parentid).Diamond_Account__c;
a.Net_One__c = acctId_to_acct.get(a.parentid).Net_One__c;
a.Account_Status__c = acctId_to_acct.get(a.parentid).Account_Status__c;
}
update childAccounts;
}
}
Avijit Chakraborty 11Avijit Chakraborty 11
ParentId is missing in the soql 

List<Account> childAccounts = [select OwnerId, salesReach__Agent_Account_Company_Name__c, salesReach__Agent_Contact_Name__c, Diamond_Account__c, Net_One__c, Account_Status__c,ParentId from Account where id in :acctId_to_acct.keyset()];
DaNae PetersonDaNae Peterson
Thank you for the reply...  I changed it.  When I test it again I get the following error now:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateChildAccount caused an unexpected exception, contact your administrator: UpdateChildAccount: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateChildAccount: line 11, column 1
Avijit Chakraborty 11Avijit Chakraborty 11
put a null pointer check before childAccounts

if(childAccounts <> NULL && childAccounts.size()>0){
for(Account a : childAccounts){
  a.Ownerid = acctId_to_acct.get(a.parentid).Ownerid;
  a.salesReach__Agent_Account_Company_Name__c = acctId_to_acct.get(a.parentid).salesReach__Agent_Account_Company_Name__c;
  a.salesReach__Agent_Contact_Name__c = acctId_to_acct.get(a.parentid).salesReach__Agent_Contact_Name__c;
  a.Diamond_Account__c = acctId_to_acct.get(a.parentid).Diamond_Account__c;
  a.Net_One__c = acctId_to_acct.get(a.parentid).Net_One__c;
  a.Account_Status__c = acctId_to_acct.get(a.parentid).Account_Status__c;
}
}
DaNae PetersonDaNae Peterson
It still throws the same error (the Null Pointer Exception)...  Anything else to try?