I am curious if it's bad practice to have unused trigger events in your trigger signature if they're never run.

For example

trigger SampleTrigger on Account (before insert, after delete) {
    if(Trigger.isAfter) {
        // rest of trigger code

I assume this would be bad, since the above trigger would run for before insert, even though no code path would be reached past the if statement.

The reason I'm asking, is because I've inherited some code that routinely adds way more trigger events than it needs to, and I'm wondering if I should spend the time to clean it up.


Ideally we should not add trigger events which are not used in the trigger code as it would execute the if condition which you have in your code and consume CPU time. In large scale applications where we have heavy code, this would impact as we have Salesforce CPU time limits.

So your assumption is wise to consider this as a bad practice.

| improve this answer | |
  • While I could see this is true. The best practice here would be to create a single trigger architecture. In large scale applications, you would have one trigger that has (only the 5 possible contexts) executes after each context but may have empty methods. Here is a simple example of that. It's also important to note, that single trigger architecture is a Salesforce best practice that they routinely recommend. I also don't believe that the CPU time for empty contexts will be affected that much. – Mike Havrilla Mar 8 '17 at 1:22
  • 3
    There's quite a few design patterns related to triggers. As the Salesforce developer, choosing the best for your situation falls upon you. Maybe the super-duper Enterprise grade trigger pattern is right for your org, or maybe something simpler is appropriate. If you are just getting started, it might be a good idea to leave some breadcrumbs for the next developer (or a future version of yourself. Maybe you want to comment out that whole if(Trigger.isAfter){...} condition. Then when you need an isAfter context, it's pretty easy to see where to put it. – John Thompson Mar 8 '17 at 1:24

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.