Is there a way to write a single helper class method to make it work for insert, update, delete, and undelete by passing trigger.newMap and trigger.oldMap to this method? e.g. count number of open opportunity records on the account. So whenever a new record is inserted, the existing record is deleted or undeleted, Opportunity record is updated to make it open.

trigger oppTrigger on Opportunity (after insert, after delete, after update) {

    oppHelper.methodName(trigger.newMap, trigger.oldMap);


public static void methodName(map<id, opportunity> nw,map<id, opportunity> ol){
//I want to keep count of number of opportunities for an account when it is updated to closed, Closed opportunity is deleted or new opportunity is insert with stage = closed

  • 3
    Yes, I imagine there is. Where specifically are you stuck? What have you tried so far? – Derek F May 23 at 0:10
  • I think this should be possible using roll-up summary on with filter criteria. – Hengky Ilawan May 23 at 1:23

it is possible, go to account. create a Rollup summary field let say 'count opportunities' . select Roll type as count and set the calculation filter like stage not equal to closed lost/closed own. (based on business req. which is open opportunities')and expose this field to account layout. so when ever the opportunities matching the filter criteria , it will get counted in Account.

so this will avoid delete scenario reason being closed opportunities will not take in to the count calculation.it will take only open opportunities.

but still a requirement to delete the closed opportunities, you can handle differently either by batch or trigger.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.