You need to sign in to do that
Don't have an account?
swain 10
Trigger to update Parent account field as rollup from Child account field (Multilevel) when a parent account is added
My scenario , when a Parent account is added to a Child account then the value(sum of all child) of one field should updated to the Parent Account Field.
Total_Lease_Area_SF_from_Children__c (Number Field) needs to get updated in parent account from the Total_Account_Child_Lease_Area_SF__c(Formula Field) of child Account.
Note:- Total_Account_Child_Lease_Area_SF__c(Formula Field) is the sum of all child till that heirrachy updated by Total_Lease_Area_SF_from_Children__c (Number Field). But When a new account is added to an existing Account parent account field is not geeting updated.
trigger AccLeaseUpdate on Account (after update, after delete) {
set<id> ParentIds = new set<id>();
if(Trigger.isupdate){
For(Account acc: Trigger.new){
ParentIds.add(acc.ParentId);
}
}
if(Trigger.isDelete){
For(Account acc: Trigger.old){
ParentIds.add(acc.ParentId);
}
}
/* List<account> accountToUpdate = new List<account>();
decimal sum = 0;
if(Trigger.isupdate || trigger.isdelete){
For(account q : [SELECT Total_Lease_Area_SF_from_Children__c ,ParentId ,(SELECT id,Total_Lease_Area_SF_from_Children__c,Total_Account_Child_Lease_Area_SF__c FROM ChildAccounts) FROM account WHERE id =: ParentIds]){
if(q.ParentId!= null && q.Total_Lease_Area_SF__c!= null){
sum = 0;
for(Account p : q.ChildAccounts){
// for(Account ap : trigger.new){
// Account myParentAcc = aq.get(p.ParentId);
sum = sum +p.Total_Lease_Area_SF_from_Children__c;
q.Total_Lease_Area_SF_from_Children__c= sum ;
// myParentAcc.Total_Lease_Area_SF_from_Children__c = sum;
}
}
try{
update accountToUpdate ;
}Catch(Exception e){
System.debug('Exception :'+e.getMessage());
}
}
accountToUpdate.add(q);
}
}*/
Total_Lease_Area_SF_from_Children__c (Number Field) needs to get updated in parent account from the Total_Account_Child_Lease_Area_SF__c(Formula Field) of child Account.
Note:- Total_Account_Child_Lease_Area_SF__c(Formula Field) is the sum of all child till that heirrachy updated by Total_Lease_Area_SF_from_Children__c (Number Field). But When a new account is added to an existing Account parent account field is not geeting updated.
trigger AccLeaseUpdate on Account (after update, after delete) {
set<id> ParentIds = new set<id>();
if(Trigger.isupdate){
For(Account acc: Trigger.new){
ParentIds.add(acc.ParentId);
}
}
if(Trigger.isDelete){
For(Account acc: Trigger.old){
ParentIds.add(acc.ParentId);
}
}
/* List<account> accountToUpdate = new List<account>();
decimal sum = 0;
if(Trigger.isupdate || trigger.isdelete){
For(account q : [SELECT Total_Lease_Area_SF_from_Children__c ,ParentId ,(SELECT id,Total_Lease_Area_SF_from_Children__c,Total_Account_Child_Lease_Area_SF__c FROM ChildAccounts) FROM account WHERE id =: ParentIds]){
if(q.ParentId!= null && q.Total_Lease_Area_SF__c!= null){
sum = 0;
for(Account p : q.ChildAccounts){
// for(Account ap : trigger.new){
// Account myParentAcc = aq.get(p.ParentId);
sum = sum +p.Total_Lease_Area_SF_from_Children__c;
q.Total_Lease_Area_SF_from_Children__c= sum ;
// myParentAcc.Total_Lease_Area_SF_from_Children__c = sum;
}
}
try{
update accountToUpdate ;
}Catch(Exception e){
System.debug('Exception :'+e.getMessage());
}
}
accountToUpdate.add(q);
}
}*/
You need a class to prevent recursive calling of the trigger. Then here is the trigger
Thanks a lot Steven Nsubuga