How can I parse a string and return an Enum value in Apex code?

I want to use Enum values ​​in my Apex code, since we have some strict types when working with an external service, however, when I get a response from an external service, I struggle to convert the string representation of the Enum value back to Enum so that it can be was used later in my code.

To do this in C #, I would do this:

DayOfWeek wednesday = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Wednesday"); 

but in Apex code I cannot find a way to do this. Anyone have a solution?

+4
source share
1 answer

This is not general, but it will work:

 String dayOfWeekNameToMatch = 'Wednesday'; DayOfWeek dayOfWeekMatch; for (DayOfWeek dow: DayOfWeek.values()) { if (dow.name() == dayOfWeekNameToMatch) { dayOfWeekMatch = dow; break; } } 
+7
source

Source: https://habr.com/ru/post/1403061/


All Articles