I want to use Enum values in my Apex code as we have some strict types when working with an external service, however when I get a response from the external service I'm struggling to convert the String Representation of the Enum value back to the Enum so it can be used later in my code.
To do this in C# I'd do this:
DayOfWeek wednesday =
(DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Wednesday");
but in Apex code I can't find a way to do this. Does anybody have a solution?
This isn't generic, but it would work:
String dayOfWeekNameToMatch = 'Wednesday';
DayOfWeek dayOfWeekMatch;
for (DayOfWeek dow: DayOfWeek.values()) {
if (dow.name() == dayOfWeekNameToMatch) {
dayOfWeekMatch = dow;
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With