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

How to update no of childcount(Field) in parentaccount detailpage its fallow accountHierarchy up to three levals using Trigger.
How to update no child count(Field) in parent account detail page its follow account hierarchy up to three levels using Trigger.
trigger NumberOfChieldCount on Account (after Insert, after Update, after delete) {
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,Nubmer_of_Chields__c from Account where id IN: ids]);
/*integer nochilds=[select count() from Account where Parentid in :ids];
system.debug('nochilds::::::::'+nochilds);
integer noofchilds=[select count() from Account where id in :ids];
system.debug('noofchilds::::::::'+noofchilds);
system.debug('accchild 27-->'+accchild);
system.debug('accparent 28-->'+accparent);
integer noof;
integer nofchids;
integer nc;*/
for(Account ac: accparent){
count =0;
for(Account acchilds: accchild){
/*if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c!=null)
nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c)+integer.valueof(ac.Nubmer_of_Chields__c);
if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c==null)
nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c);
system.debug('noof:::: 36'+nofchids );*/
if(acchilds.Parentid == ac.id)
count++;
else
count--;
}
//ac.Nubmer_of_Chields__c =string.valueof(count);
/*system.debug('ac.Nubmer_of_Chields__c::::::::'+ac.Nubmer_of_Chields__c);
system.debug('count:::: 47'+count);
system.debug('count:::: 47'+nofchids);*/
//if(count>0 && nofchids!=null)
//ac.Nubmer_of_Chields__c =string.valueof(count+nofchids);
//if(count>0 && nofchids==null)
//if(ac.Parentid == ac.id)
ac.Nubmer_of_Chields__c =string.valueof(count);
/*else
ac.Nubmer_of_Chields__c =string.valueof(count); */
}
try{
upsert accparent;
}catch(DMLException ex){
System.debug('Exception is '+ex);
}
}
}
Note:Create new field Nubmer_of_Chields__c in Account(Object).
A-->No of child are 2+1 =3
B--->2
c---->1
trigger NumberOfChieldCount on Account (after Insert, after Update, after delete) {
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,Nubmer_of_Chields__c from Account where id IN: ids]);
/*integer nochilds=[select count() from Account where Parentid in :ids];
system.debug('nochilds::::::::'+nochilds);
integer noofchilds=[select count() from Account where id in :ids];
system.debug('noofchilds::::::::'+noofchilds);
system.debug('accchild 27-->'+accchild);
system.debug('accparent 28-->'+accparent);
integer noof;
integer nofchids;
integer nc;*/
for(Account ac: accparent){
count =0;
for(Account acchilds: accchild){
/*if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c!=null)
nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c)+integer.valueof(ac.Nubmer_of_Chields__c);
if(acchilds.Nubmer_of_Chields__c!=null && ac.Nubmer_of_Chields__c==null)
nofchids=integer.valueof(acchilds.Nubmer_of_Chields__c);
system.debug('noof:::: 36'+nofchids );*/
if(acchilds.Parentid == ac.id)
count++;
else
count--;
}
//ac.Nubmer_of_Chields__c =string.valueof(count);
/*system.debug('ac.Nubmer_of_Chields__c::::::::'+ac.Nubmer_of_Chields__c);
system.debug('count:::: 47'+count);
system.debug('count:::: 47'+nofchids);*/
//if(count>0 && nofchids!=null)
//ac.Nubmer_of_Chields__c =string.valueof(count+nofchids);
//if(count>0 && nofchids==null)
//if(ac.Parentid == ac.id)
ac.Nubmer_of_Chields__c =string.valueof(count);
/*else
ac.Nubmer_of_Chields__c =string.valueof(count); */
}
try{
upsert accparent;
}catch(DMLException ex){
System.debug('Exception is '+ex);
}
}
}
Note:Create new field Nubmer_of_Chields__c in Account(Object).
A-->No of child are 2+1 =3
B--->2
c---->1
Try below code.
Mark this as the best answer if this resolve your issue.
Thanks!
Chirag
I have tried with the above code, is not updating, could you please update the code.
Thanks!
I have tried with the above code, is not updating, could you please update the code.
Note: only one level parent account child account records got it in debug, UI is not working, But child account to parent accounts to parent. the parent accounts not updating/found in debug/UI.
Thanks!
Hemant