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();
system.debug(fields.containsKey('CaseNumber'));
system.debug(fields.containsKey('CASENUMBER'));
system.debug(fields.containsKey('casenumber'));
system.debug(fields.containsKey('CaSeNuMbEr'));
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)
{
fields.add(member.getFieldPath());
}
system.debug(fields.contains('CaseNumber');
Enum
for this requirement? Looks like you are trying to representSObjectField
, which is a much more appropriate token type if that's what you are after. – Adrian Larson♦ Mar 12 '18 at 15:29