function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jayesh pagarejayesh 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;
}
 
Agustina GarciaAgustina Garcia
Do you need the code for any reason? You can get the same in a declarative way.

Creates a new Rollup field on Account 

User-added image
And select Sum and the field of Opportunity you want to sum

User-added image

Hope this helps
Agustina
jayesh pagarejayesh pagare
Thank You Agustina,But my issue is our limit with rollup field is over with max 25 so I have done with apex trigger.
Agustina GarciaAgustina Garcia
Sorry for the late response. Actually your issue is that when you remove your latest Opportunity, the aggregate variable is empty, so it does not do anything. Try this:
 
trigger Account_Total_Amount on Opportunity (after delete)
{
    List<Account> accList = new list<Account>();
	Set<id> accountIDs  = new set<id>();
    Set<Id> oppIds = new Set<Id>();
 
	if(trigger.isDelete)
    {
		for(Opportunity o : Trigger.old)
        {
            oppIds.add(o.Id);
			accountIDs.add(o.Accountid);
		}
	
		if(Trigger.isAfter)
        {
            AggregateResult[] groupedResults = [SELECT SUM(Amount), AccountId FROM opportunity where AccountId IN :AccountIDs GROUP BY AccountId ];
          
            system.debug('*******groupedResults **********'+groupedResults);      
            if(groupedResults.size() > 0)
            {
                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);
                }
            }
            else //There is no single element
            {
                Map<Id,Account> accMap = new Map<Id,Account>([Select Id, Total_Amount__c From Account Where Id IN :accountIDs]);
				for(Id retrievedAccIds : accountIDs)
                {
                    Account acc = accMap.get(retrievedAccIds);
                    acc.Total_Amount__c = 0;
                    accList.add(acc);
                }
            }
            update accList;
        }
	}

	
}