Enums

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 that don’t otherwise have a numerical order. Typical examples include the suit of a card, or a particular season of the year.

Although each value corresponds to a distinct integer value, the enum hides this implementation. Hiding the implementation prevents any possible misuse of the values to perform arithmetic and so on. After you create an enum, variables, method arguments, and return types can be declared of that type.

Unlike Java, the enum type itself has no constructor syntax.

Note

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 would any other data type. For example:

Season southernHemisphereSeason = Season.WINTER;

public Season getSouthernHemisphereSeason(Season northernHemisphereSeason) {

    if (northernHemisphereSeason == Season.SUMMER) return southernHemisphereSeason;
     //...
}

You can also define a class as an enum. When you create an enum class, do not use the class keyword in the definition.

public enum MyEnumClass { X, Y }

You can use an enum in any place you can use another data type name. If you define a variable whose type is an enum, any object you assign to it must be an instance of that enum class.

Any webservice method can use enum types as part of their signature. In this case, the associated WSDL file includes definitions for the enum and its values, which the API client can use.

Apex provides the following system-defined enums:

  • System.StatusCode

    This enum 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

    The full list of status codes is available in the WSDL file for your organization. For more information about accessing the WSDL file for your organization, see Downloading Salesforce WSDLs and Client Authentication Certificates in Salesforce Help.

  • System.XmlTag:

    This enum returns a list of XML tags used for parsing the result XML from a webservice method. For more information, see XmlStreamReader Class.

  • System.ApplicationReadWriteMode: This enum indicates if an organization is in 5 Minute Upgrade read-only mode during Salesforce upgrades and downtimes. For more information, see System.getApplicationReadWriteMode().
  • System.LoggingLevel:

    This enum is used with the system.debug method, to specify the log level for all debug calls. For more information, see System Class.

  • System.RoundingMode:

    This enum is used by methods that perform mathematical operations to specify the rounding behavior for the operation. Typical examples are the Decimal divide method and the Double round method. For more information, see Rounding Mode.

  • System.SoapType:

    This enum is returned by the field describe result getSoapType method. For more information, see SOAPType Enum.

  • System.DisplayType:

    This enum is returned by the field describe result getType method. For more information, see DisplayType Enum.

  • System.JSONToken:

    This enum is used for parsing JSON content. For more information, see JsonToken Enum.

  • ApexPages.Severity:

    This enum specifies the severity of a Visualforce message. For more information, see ApexPages.Severity Enum.

  • Dom.XmlNodeType:

    This enum specifies the node type in a DOM document.

System-defined enums cannot be used in Web service methods.

Note

All enum values, including system enums, have common methods associated with them. For more information, see Enum Methods.

You cannot add user-defined methods to enum values.