0 votes
1 view
in Salesforce by (11.9k points)

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?

1 Answer

0 votes
by (31.6k points)

This isn't generic, but it should work:

String dayOfWeekNameToMatch = 'Wednesday';

DayOfWeek dayOfWeekMatch;

for (DayOfWeek dow: DayOfWeek.values()) {

    if (dow.name() == dayOfWeekNameToMatch) {

        dayOfWeekMatch = dow;

        break;

    }

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...