Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (32.1k 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;

    }

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...