You need to sign in to do that
Don't have an account?
Janno 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;
}
}
}
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;
}
}
}
Use the below code
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