1

I'm looking into a simple approach many Triggers into one.

Which approach will work best...

Example #1

trigger OpportunityTrigger on SomeObject (after insert, before delete) {
  //in this case both insert/delete call the same class logic
  if(Trigger.isAfter && Trigger.isInsert || Trigger.isBefore && Trigger.isDelete) {
    // Call class logic here! 
  }

}

Example #2

trigger OpportunityTrigger on SomeObject (after insert, before delete) {

  if (Trigger.isAfter) {
    if (Trigger.isInsert) {
      // Call class logic here!
    } 
  }
  if (Trigger.isBefore) {
    if (Trigger.isDelete) {
      // Call class logic here!
    } 
  }
}
2

1 Answer 1

0

I find your second approach preferable as you can allow the code to scale.

Also you can delegate the actual trigger processing to a separate Service Class roughly like below.

trigger OpportunityTrigger on SomeObject (after insert, before delete) {
    if ( Trigger.isAfter && Trigger.isInsert ) {
        Service.method1();
    }
    if ( Trigger.isBefore && Trigger.isDelete ) {
        Service.method2();
    }
}

public class Service {
    method1() { /* do stuff */ }
    method2() { /* do stuff */ }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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