You need to sign in to do that
Don't have an account?

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!
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!
(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;
}
}
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()];
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
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;
}
}