0

My trigger scenario:

Once an account will update then that Account will update with the total amount from All Opportunities on Account level. Account field name would be Total Opportunity Amount.

Create a custom field called Total Opportunity Amount on Account object.

Trigger object: Account Trigger event : before update

Note: No DML statements is required for before update.

My implementation:

trigger accTrigger on Account(before update)
{
    switch on Trigger.OperationType
    {
        when BEFORE_UPDATE
        {
            accHandler.updateTotalOppAmt(Trigger.new);
        }
    }
}
public class accHandler
{
    public static void updateTotalOppAmt(List<Account> lstAcc)
    {
        Set<Id> accIds = new Set<Id>();
        for(Account ac : lstAcc)
        {
            accIds.add(ac.Id);
        }

        List<AggregateResults> agResult = [Select Id, Sum(Amount) totalAmt, AccountId from Opportunity where AccountId in :accIds group by Id, AccountId];

        Map<Id, Decimal> mapTotalOppAmt = new Map<Id, Decimal>();

        for(AggregateResults ag : agResult)
        {
            Id accountId = (Id)ag.get('AccountId');
            Decimal oppAmt = (Decimal)ag.get('totalAmt');
            mapTotalOppAmt.put(accountId, oppAmt);
        }

        for(Account ac : lstAcc)
        {
            if(mapTotalOppAmt.containsKey(ac.Id));
            {
                ac.Total_Opp_Amount__c = mapTotalOppAmt.get(ac.Id);
            }
        }
    }
}

Here, two objects are involved: Account and Opportunity. Could you please explain why we need to use "before update" instead of "after update" in my trigger scenario? Any clarification would be greatly appreciated.

2
  • 1
    As a general note, you should pay more attention to your code style and formatting. Having random amounts of blank lines, poor indentation, missing spaces between operators, etc... makes it very hard to read code. Poor discipline in code style is going to make it easier to make mistakes and harder to fix them, especially 6 months in the future when you've forgotten about what this code does but need to revisit it to fix or change something.
    – Derek F
    Mar 2 at 13:19
  • 2
    This rollup implementation is also incomplete (for example, what happens when an Opportunity is moved to a different Account? What happens when an Account loses its last Opportunities?). In general, you should be using the DLRS tool instead of writing your own rollup code. In this specific case, you should instead be using a Rollup Summary Field (a standard feature that Salesforce provides).
    – Derek F
    Mar 2 at 13:24

1 Answer 1

1

The quick answer is that we don't need to use a before trigger for rollups
but it does save us some trouble

Aside from the obvious benefit of not needing an explicit DML statement (and thus not consuming extra DML statements/rows (and more) of our governor limit), using a before trigger here prevents you from getting yourself into an infinite loop.

Think about it for a moment. If you're updating the Accounts in trigger.new (well, different instances of the same records) inside of an Account trigger, that's going to cause your Account trigger to run again. Then you do the work again, and update the same accounts again, which gets you back into the trigger, and the cycle repeats. There's nothing in your code (currently) that is able to break that cycle.

There are ways to mitigate that issue, but the question then becomes why would you want to write more code to accomplish something if you can accomplish the same thing with less code?

Taken to the logical conclusion here, that means you shouldn't write any code for this and use a Rollup Summary field instead. Barring that, using a before trigger is the way to go.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .