You need to sign in to do that
Don't have an account?
jayesh pagare
Last Child Record Value on parent object remain as it is even after it is deleted
Hi Friends,
My screnaio is I am using custom rollup on account to calculate sum of total amount of all child opportunities(amount field).In my case trigger is working fine if i have more that 2 opportunities and i delete one opportunity.But if the account has only one opportunity as child then even after deleting that single opportunity the total amount field on account does not showing zero amount instead showing amount of last opportunity.
Here is my Trigger.
trigger Account_Total_Amount on Opportunity (after delete) {
List<Account> accList = new list<Account>();
Set<id> accountIDs = new set<id>();
if(trigger.isDelete){
for(Opportunity o : Trigger.old){
accountIDs.add(o.Accountid);
}
}
AggregateResult[] groupedResults = [SELECT SUM(Amount), AccountId FROM opportunity where AccountId IN :AccountIDs GROUP BY AccountId ];
system.debug('*******groupedResults **********'+groupedResults);
for(AggregateResult ar:groupedResults) {
Id accid = (ID)ar.get('AccountId');
Decimal count = (Decimal)ar.get('expr0');
Account acc = new Account(Id=accid);
acc.Total_Amount__c = count;
accList.add(acc);
}
update accList;
}
My screnaio is I am using custom rollup on account to calculate sum of total amount of all child opportunities(amount field).In my case trigger is working fine if i have more that 2 opportunities and i delete one opportunity.But if the account has only one opportunity as child then even after deleting that single opportunity the total amount field on account does not showing zero amount instead showing amount of last opportunity.
Here is my Trigger.
trigger Account_Total_Amount on Opportunity (after delete) {
List<Account> accList = new list<Account>();
Set<id> accountIDs = new set<id>();
if(trigger.isDelete){
for(Opportunity o : Trigger.old){
accountIDs.add(o.Accountid);
}
}
AggregateResult[] groupedResults = [SELECT SUM(Amount), AccountId FROM opportunity where AccountId IN :AccountIDs GROUP BY AccountId ];
system.debug('*******groupedResults **********'+groupedResults);
for(AggregateResult ar:groupedResults) {
Id accid = (ID)ar.get('AccountId');
Decimal count = (Decimal)ar.get('expr0');
Account acc = new Account(Id=accid);
acc.Total_Amount__c = count;
accList.add(acc);
}
update accList;
}
Creates a new Rollup field on Account
And select Sum and the field of Opportunity you want to sum
Hope this helps
Agustina