Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code used in "Enums in Apex | What is Enum and where do we use it? | Salesforce Apex Tutorial Series by SFDC Stop" tutorial on SFDC Stop (https://youtu.be/N3du8bRbYSs)
-------------
Enums in Apex
--------------
An enum is an abstract data type whose values consist of a finite set of identifiers.
Enums are typically used to define a set of possible values that dont otherwise have a numerical order.
public enum Color { RED, BLUE, GREEN, ORANGE, PURPLE }
values()
---------
public enum Color { RED, BLUE, GREEN, ORANGE, PURPLE }
List<Color> colorEnums = Color.values();
System.debug(colorEnums);
ordinal()
----------
public enum Color { RED, BLUE, GREEN, ORANGE, PURPLE }
System.debug(Color.BLUE.ordinal());
System.debug(Color.PURPLE.ordinal());
name()
--------
public enum Color { RED, BLUE, GREEN, ORANGE, PURPLE }
System.debug('BLUE'.equals(Color.BLUE.name()));
valueOf()
--------
public enum Color { RED, BLUE, GREEN, ORANGE, PURPLE }
Color blueColor = Color.valueOf('blue1');
System.debug(blueColor);
System.debug('Logging Levels: ' + LoggingLevel.values());
System.debug('Status Codes: ' + StatusCode.values());
primary Student - 0
secondary Student - 1
senior secondary student - 2
0, 1, 2
Student student1 = new Student(1, 'Richard', Student.Category.SECONDARY);
System.debug(student1.getCategory());
System.debug(student1.getCategoryAsString());
--------------------------------------------------------------------------------
Student Class
--------------------------------------------------------------------------------
public class Student {
public enum Category { PRIMARY, SECONDARY, SENIOR_SECONDARY }
Integer id;
String name;
Category studentCategory;
public Student(Integer id, String name, Category studentCategory) {
this.id = id;
this.name = name;
this.studentCategory = studentCategory;
}
public Category getCategory() {
return studentCategory;
}
public String getCategoryAsString() {
return studentCategory.name();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment