In Apex, enums (enumerations) are a particular type that defines a set of named constants. Enums help represent fixed sets of related values, providing clarity, type safety, and ease of maintenance in your code. Salesforce supports enums in Apex, allowing developers to define custom data types with predefined values.
Here's how you can define and use enums in Salesforce Apex:
Syntax for Enum Declaration:
public enum PartnerType {
SILVER,
GOLD,
PLATINUM
}
Usage Examples:
Declaring Enum Variables:
PartnerType status = PartnerType. SILVER;
Switch Statements:
switch(status) {
case PartnerType. SILVER:
// Do something
break;
case PartnerType. GOLD:
// Do something else
break;
default:
// Default case
}
Iterating Over Enum Values:
for (PartnerType p : PartnerType.values()) {
System.debug(p);
}
Using Enums in Methods:
public void processStatus(PartnerType status) {
switch(status) {
case PartnerType. SILVER:
// Process SILVER
break;
case PartnerType. GOLD:
// Process GOLD
break;
case PartnerType. PLATINUM:
// Process PLATINUM
break;
// Handle other cases as needed
}
}
Here is the list of 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
System.XmlTag:
This enum returns a list of XML tags used for parsing the result XML from a webservice method.
System.ApplicationReadWriteMode
This enum indicates if an organization is in 5 Minute Upgrade read-only mode during Salesforce upgrades and downtimes.
System.LoggingLevel:
This enum is used with the system.debug method, to specify the log level for all debug calls.
System.RoundingMode:
This enum is used by methods that perform mathematical operations to specify the rounding behavior for the operation.
System.SoapType:
This enum is returned by the field describe result getSoapType method.
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.
All Apex enums, whether user-defined enums or built-in enums, have these standard methods:
values
This method returns the values of the Enum as a list of the same Enum type.
valueOf(string enumStr)
This method converts a specified string to an enum constant value. An exception is thrown if the input string doesn't match an enum value.
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 item's position as an Integer in the list of Enum values starting with zero.
Example - Use of Enum name and ordinal methods
public enum DayOfWeek {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
// Custom method to return the ordinal value
public Integer ordinal() {
DayOfWeek[] values = DayOfWeek.values();
for(Integer i = 0; i < values.size(); i++) {
if(values[i] == this) {
return i;
}
}
return -1; // Return -1 if the enum constant is not found
}
// Custom method to return the name of the enum constant
public String name() {
return String.valueOf(this);
}
}
// Usage example
DayOfWeek today = DayOfWeek.MONDAY;
System.debug('Ordinal value of ' + today.name() + ': ' + today.ordinal()); // Output: Ordinal value of MONDAY: 1
Note—System-defined enums cannot be used in Web service methods, and user-defined methods cannot be added to enum values.
References
Comments
Post a Comment