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

Trigger to count the number of Child Accounts in hierarchy
Hi guys,
I wrote a trigger to count the number of child Accounts in the hierarchy and store it in a custom field in the Parent Account.
Trigger works fine, except when I remove the parent-child mapping from one of the child Accounts ("deparent"). The count does not go down.
I'm pretty sure I'm overlooking something here. Any help appreciated.
Thank you !!
I wrote a trigger to count the number of child Accounts in the hierarchy and store it in a custom field in the Parent Account.
Trigger works fine, except when I remove the parent-child mapping from one of the child Accounts ("deparent"). The count does not go down.
I'm pretty sure I'm overlooking something here. Any help appreciated.
Thank you !!
trigger countChildAcc on Account (after Insert, after Update) { Set<Id> Ids= new Set<Id>(); List<Account> acclist = new List<Account>(); Integer count = 0; if(Trigger.isInsert || Trigger.isUpdate){ for(Account acc: Trigger.new){ if(acc.ParentId!=null) Ids.add(acc.ParentId); acclist.add(acc); } } if(Trigger.isDelete){ for(Account acc: Trigger.old){ if(acc.ParentId!=null) Ids.add(acc.ParentId); acclist.add(acc); } } if(Ids.size()>0){ List<Account> accChild = new List<Account>([SELECT Id,ParentId FROM Account WHERE ParentId IN: Ids]); List<Account> accParent = new List<Account>([SELECT Id,No_of_Child_Accounts__c FROM Account WHERE Id IN: Ids]); for(Account ac: accParent){ count =0; for(Account acChild: accChild){ if(acChild.ParentId == ac.Id) count++; } ac.No_of_Child_Accounts__c = count; } try{ upsert accParent; }catch(DMLException ex){ System.debug('Exception is '+ex); } } }
in place of "Trigger.isDelete" please use "Trigger.isUpdate".
it will resolve your issue.
PFB the working code:
---------------------------------
trigger countChildAcc on Account (after Insert, after update, before delete) {
Set<Id> Ids= new Set<Id>();
Integer count = 0;
if(Trigger.isInsert){
system.debug('Trigger.new >>>'+Trigger.new);
for(Account acc: Trigger.new){
if(acc.ParentId!=null)
Ids.add(acc.ParentId);
}
system.debug('Ids in Insert >>>'+Ids);
}
if(Trigger.isUpdate){
system.debug('Update Trigger.new>>>>'+Trigger.new);
for(Account acc: Trigger.new){
Id OldParentId = Trigger.oldMap.get(acc.Id).ParentId;
system.debug('OldParentId>>>>'+OldParentId);
system.debug('acc.ParentId>>>>'+acc.ParentId);
if(acc.ParentId==null && Trigger.oldMap.get(acc.Id).ParentId !=null){
Ids.add(OldParentId);
}else if(acc.ParentId!=null){
Ids.add(acc.ParentId);
}
system.debug('Update Ids >>>'+Ids);
}
}
if(Trigger.isDelete){
system.debug('Trigger.old >>>'+Trigger.old);
for(Account acc: Trigger.old){
if(acc.ParentId!=null)
Ids.add(acc.ParentId);
}
system.debug('Ids in delete >>>'+Ids);
}
if(Ids.size()>0){
List<Account> accChild = new List<Account>([SELECT Id, ParentId FROM Account WHERE ParentId IN: Ids]);
system.debug('accChild List >>>'+accChild);
List<Account> accParent = new List<Account>([SELECT Id, Child_Accounts__c FROM Account WHERE Id IN: Ids]);
system.debug('accParent List >>>'+accParent);
for(Account ac: accParent){
count =0;
for(Account acChild: accChild){
if(acChild.ParentId == ac.Id)
count++;
}
system.debug('count >>>'+count);
ac.Child_Accounts__c = count;
}
try{
system.debug('Try accParent >>>'+accParent);
upsert accParent;
}catch(DMLException ex){
System.debug('Exception is '+ex);
}
}
}