In My ENUM a list of values is added and I need to check if certain string values are in my ENUM and which is dynamically checked through a loop.

public Enum KeyValues{CaseNumber, Name, OwnerId, Id}

Need to check if keyValues contains Name

If('Name' == keyvalues--)// how to check if 'Name' is in ENUM - KeyValues)
  • Likely duplicate of: Converting/Parsing an Enumeration to a String More Generically
    – Adrian Larson
    Mar 12, 2018 at 15:23
  • Also why are you using an Enum for this requirement? Looks like you are trying to represent SObjectField, which is a much more appropriate token type if that's what you are after.
    – Adrian Larson
    Mar 12, 2018 at 15:29
  • yeah but I don't want certain fields (few fields) to be included and it varies from object to object, so specified those fields in Enum and excluding that fields
    – SFDC
    Mar 13, 2018 at 7:14
  • Hence field set...
    – Adrian Larson
    Mar 13, 2018 at 13:38

2 Answers 2


You can use the values method to get all values of enum, then check name of each in loop.

Set<String> enumNames = new Set<String>();
for (KeyValues keyValue : KeyValues.values()){

    // do something
  • thanks @alexander :) some other way without using a loop is preferable because am gonna check if the enum contains a string inside a for loop, so it's not the best practice to use a loop inside a loop right ?? it has to loop through each and every time. Please tell me if there any other way to achieve this, thanks - If not Will stick with this method
    – SFDC
    Mar 12, 2018 at 13:30
  • 1
    @dinesh if you have to perform checking multiple times, just collect values of enum in set. See my updated answer Mar 12, 2018 at 13:35
  • Yeah mate :) got it thanks for the Updated answer and your time :)
    – SFDC
    Mar 13, 2018 at 5:11

If you want to check whether or not a String value corresponds to a valid field name, you should use describes to get the field map. That way you don't have to maintain all the supported values, and you can support more than 100 fields. As a bonus, it's actually case-insensitive.

Map<String, SObjectField> fields = SObjectType.Case.fields.getMap();

If you want to support just a subset of all fields, use a Field Set. This approach is case-sensitive unless you build in some additional logic.

Set<String> fields = new Set<String>();
for (FieldSetMember member : SObjectType.Case.fieldsets.MyFieldSet)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .