Any examples how to use new System.TriggerOperation enum in a trigger? How this code could be replaced:

trigger TriggerName on ObjectName (before delete, before insert, before update,   after delete, after insert, after update) {

    if (Trigger.isBefore) {
       if (Trigger.isInsert) { }

According to Summer 18 Apex developer guide, 7 new Enum values are introduced in System.TriggerOperation class.


Similarly, a new variable is added in Trigger class named as 'operationType, which stores the context enum it is executing in.

Thus your trigger using switch case will be like :

trigger AccountTrigger on Account (before delete, before insert, before update,   after delete, after insert, after update) {

    switch on Trigger.operationType{
        when AFTER_INSERT{
            //do after insert stuff
        when AFTER_UPDATE{
            //Do after update stuff


there is no fall-through. After the code block is executed for a particular when block, the switch statement exits

Thus you don't have to add break statement, adding break will cause compilation issues.

Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_enum_System_TriggerOperation.htm?search_text=TriggerOperation

Source: https://developer.salesforce.com/blogs/2018/05/summer18-rethink-trigger-logic-with-apex-switch.html

  • 1
    Oh, I thought that enum was a joke. That's seriously useful. Time to update all my triggers...
    – sfdcfox
    Jun 9 '18 at 21:02
  • 1
    @sfdcfox even I thought same, Then I saw Daniel Ballinger's idea with delivered tag. Jun 9 '18 at 21:08
  • @sfdcfox Does this mean that using switch with an enum is a less-resource intensive activity than using the if/else/else/else/else, etc pattern that some use? Jun 25 '18 at 20:05
  • Also, I have to wonder if this is a good idea because one would have to store, for example, Trigger.new in the handler class - and if you want to handle every possible enum, you have to store Trigger.new, Trigger.newMap, Trigger.old, and Trigger.oldMap. Then how do changes to those local variables get populated back to the original passed-in lists and maps? While switch is nice, I'm unsure that it's going to be a best practice in a trigger handler. Thoughts? Jun 25 '18 at 20:08
  • @DavidSchach You might want to post a question with a full example, but I feel you have a fundamental misunderstanding (or two) about trigger handlers, passing variables by reference, etc.
    – sfdcfox
    Jun 25 '18 at 20:18

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.