Skip to content

Instantly share code, notes, and snippets.

@rahulmalhotra
Created December 7, 2021 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulmalhotra/521c8f077755325c672df8ce3ef5f3b1 to your computer and use it in GitHub Desktop.
Save rahulmalhotra/521c8f077755325c672df8ce3ef5f3b1 to your computer and use it in GitHub Desktop.
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 don’t 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