Hello Everyone, In this tutorial, I am going to illustrate the Enum data type, which is a very important part of Apex.


After completing this unit, you'll able to:
  • What is Enum?
  • Defining Enum.
  • Describe Enum Methods

So let's begin,

What is Enum?

An enum is an abstract data type with values that each take on exactly one of a finite set of identifiers that you specify. You can typically use enums to define a set of possible values. When you create an enum, variables, method arguments, and their return types can be declared of that type. In Salesforce apex provides built-in enums, such as LoggingLevel. You can also define your own enum data type in an apex. In enum 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.


Defining Enum
Define an enum in your apex class, you have to use enum keyword in your declaration and then use curly braces to demarcate the list of possible values.

For example, the following code creates an enum called OrderType:
public enum OrderType{ Coop, Normal, Package, SubInvoicing }
By creating the enum OrderType, you have also created a new data type called OrderType. You can use this new data type as you might use any other data type.

For Example:
OrderType o = OrderType.Coop; OrderType m(Integer x, OrderType o){ if(o==OrderType.Normal) return o; //…. }
Class as Enum

This is the interesting point of this enum is you can define a class as an enum. When you create an enum class you do not use the class keyword in the declaration.
public enum MyEnumClass{X,Y}
When you define a variable whose type is an enum, any object that you assign it must be an instance of that enum class.

System Defined Enums

Apex provides the following types of system-defined enums:

1. System.StatusCode: corresponds to the API error code that is exposed in the WSDL document for all API operations.

For example:
StatusCode.CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
StatusCode.INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY
List of all status codes is available in the WSDL file for your organization.

2. System.XmlTag: returns a list of XML tags used for parsing the result XML from a web service method.

3. System.LoggingLevel: used with the system.debug method, to specify the log level for all debug calls.

4. System.ApplicationReadWriteMode: indicates if an organization is in 5 Minute Upgrade read-only mode during Salesforce upgrades and downtimes.

5. System.SoapType: returned by the field describe result getSoapType method.

6. System.JSONToken: used for parsing JSON content.

7. Dom.XmlNodeType: specifies the node type in a DOM document.

8. ApexPages.Severity: specifies the severity of a Visualforce message.

9. System.DisplayType: returned by the field describe result getType method.

10. System.RoundingMode: used by methods that perform mathematical operations to specify the rounding behavior for the operation, such as the Decimal divide method and the Double round method.

Enum Methods

Enum has the following methods that take no arguments.

1. values: returns the values of the Enum as a list of the same Enum type.
Example:
List values = StatusCode.values(); System.debug(values);
2. name: returns the name of the Enum item as a String.
Example:
String s = StatusCode.DELETE_FAILED.name(); System.debug(s);
3. ordinal: returns the position of the item, as an Integer, in the list of Enum values starting with zero.
Example:
Integer i = StatusCode.DELETE_FAILED.ordinal(); System.debug(i);
Note: Enum values cannot have user-defined methods added to them.

See also:
Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.