Tag Archives: Enum

Salesforce Apex TriggerOperation Enum

The System.TriggerOperation enum has the following values:

  • BEFORE_INSERE
  • AFTER_INSERT
  • BEFORE_UPDATE
  • AFTER_UPDATE
  • BEFORE_DELETE
  • AFTER_DELETE
  • AFTER_UNDELETE

The new Trigger context variable Trigger.operationType will return System.TriggerOperation enum during trigger context.

If you combine this new context variable and the new Apex switch feature, trigger code becomes much easy to implement and understand.

Sample Switch and enum in Triggers:

trigger AccountTrigger on Account(before insert, after insert, before update, after update, before delete, after delete, after undelete) {
    
    switch on Trigger.operationType {
        
        when BEFORE_INSERT {
            //Invoke before insert trigger handler
            system.debug('Before Insert');
        }
        when AFTER_INSERT {
            //Invoke after insert trigger handler
            system.debug('After Insert');
        }
        when BEFORE_UPDATE {
            //Invoke before update trigger handler
            system.debug('Before Update');
        }
        when AFTER_UPDATE {
            //Invoke after update trigger handler
            system.debug('After Update');
        }
        when BEFORE_DELETE {
            //Invoke before delete trigger handler
            system.debug('Before Delete');
        }
        when AFTER_DELETE {
            //Invoke after delete trigger handler
            system.debug('After Delete');
        }
        when AFTER_UNDELETE {
            //Invoke after undelete trigger handler
            system.debug('After Undelete');
        }
    }
}

Enum in Apex

Biswajeet   October 20, 2017   No Comments on Enum in Apex

An enum is an abstract data type with values that each take on exactly one of a finite set of identifiers that you specify. Enums are typically used to define a set of possible values.

Although each value corresponds to a distinct integer value, the enum hides this implementation so that you don’t inadvertently misuse the values, such as using them to perform arithmetic. After you create an enum, variables, method arguments, and return types can be declared of that type. Apex provides built-in enums, such as LoggingLevel, and you can define your own enum.

To define an enum, use the enum keyword in your declaration and use curly braces to demarcate the list of possible values.

For Example:
The following code creates an enum called Season:

public enum Season {WINTER, SPRING, SUMMER, FALL}

By creating the enum Season, you have also created a new data type called Season. You can use this new data type as you might any other data type.

For Example:

Season e = Season.WINTER;

Season CheckSeason(Season e) {
    if (e == Season.SUMMER){
		return e;
	}
} 

Enum Methods:
All Apex enums, whether user-defined enums or built-in enums, have the following common method that takes no arguments.

values : This method returns the values of the Enum as a list of the same Enum type. Each Enum value has the following methods that take no arguments.
name : Returns the name of the Enum item as a String.
ordinal : Returns the position of the item, as an Integer, in the list of Enum values starting with zero.
Note: Enum values cannot have user-defined methods added to them.
For Example:

Integer i = StatusCode.DELETE_FAILED.ordinal();
String s = StatusCode.DELETE_FAILED.name();
List<StatusCode> values = StatusCode.values();