I have a hole in the logic of a huge Trigger that "piggybacks" data from Opportunities to Account (summarized). If someone touches an Opp, this trigger fires and checks ALL OPPS under that Account for "that Product" (a field on the Opp) and summarizes the data into 5-10 fields on the Account.

Issue: I do not have proper logic in place so that if an Opp is deleted, this Trigger would fire and do "it's normal job" when that was the ONLY OPP for that Product. It works fine if at least one Opp still exists for that product.

Logic tells me that I need to run an "after delete" trigger which would run THIS TRIGGER, but the problem is, this trigger requires an Opp (at least one) to work with. If no Opp exists, this Trigger would do nothing.

The fields that could be affected vary depending on what options are selected for that Product. I'm fearful that I may need to write a cut-down version of the trigger JUST FOR After Delete to clear all fields if NO Opps exist for that Product any longer (a LOT of new territory for me - including how to reference in an After Delete fields on the record just deleted...). I could probably just encapsulate said logic in an ELSE where the original trigger is the IF THEN (1+ opps exist, ELSE do cleanup routine).

Any ideas? The entire basis of this 500 line trigger is that one or more Opps exist (for the product in question), but I need it to cleanup up (basically NULL) the data for that Product on Account if the one and only Opp for that product is deleted.

  • if sounds like the same logic should execute after delete / insert / update, i.e. recalculate from scratch the aggregate values. if after delete no opps are found, set values to zero? Apr 11, 2013 at 15:25
  • Depending on the org, normally an opp wouldn't just be "deleted"; instead it would be Closed as 'Closed Lost'. I suspect you really need to talk with your architect or business people to find out how they actually use SF opportunities before continuing.
    – crmprogdev
    Apr 11, 2013 at 15:48
  • By virtue of my position and reliance upon me, I am now the "Architect". They are definitely being deleted. NOT OFTEN, but it does happen, especially when Accounts are merged and there are extra Opps. (point being, I walked INTO this SF Org 3 years ago, it was created 4.5 years ago)
    – AMM
    Apr 11, 2013 at 16:10

2 Answers 2


Here is my stab at this. If it is a delete trigger, the values on the Account will be set to 0, since Accounts instantiated will have 0 values. I've switched to using Trigger.old in case of a delete trigger.

trigger OpportunityAfter on Opportunity(after insert, after delete, after update) {
    Map<Id, Account> aggregateValMap = new Map<Id, Account>();

    for (Opportunity opp : Trigger.isDelete ? Trigger.old : Trigger.new) {
        Account acc = aggregateValMap.get(opp.AccountId);
        if (acc == null) {
            // initialise aggregates to 0
            acc = new Account(Id = opp.AccountId, Aggregate1__c = 0, Aggregate2__c = 0, ...);
            aggregateValMap.put(opp.AccountId, acc);

        if (!Trigger.isDelete) { // do not add values if record is deleted
            acc.Aggregate1__c += opp.Aggregate1__c;
            acc.Aggregate2__c += opp.Aggregate2__c;

    if (!aggregateValMap.isEmpty())
  • Ugh. I guess I should post my Trigger (though I'll have to cut unimportant areas as it's larger than is allowed here). Your code is not ringing anything in my head that would make sense. :-/
    – AMM
    Apr 11, 2013 at 17:41
  • 1
    @AMM I'm conditionally using trigger.old if it is an after delete trigger so that it enters the loop to set the aggregate fields on the associated Account to 0. Apr 11, 2013 at 19:20
  • Okay, I'm sort of getting it.. there is some syntax I'm not familiar with that I need to dig up explanations for (such as your for loop with the ?, etc). Thanks.........
    – AMM
    Apr 11, 2013 at 19:28
  • 1
    Its the ternary operator if-then-else salesforce.com/us/developer/docs/apexcode/Content/… Apr 11, 2013 at 20:15

Great information offered here. Based on my needs and only low-level developer knowledge to date (plus timelines involved), I'm using a solution provided here.

Simple-to-use "if field exists" logic?

Thank you both for your input!

You must log in to answer this question.

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