[SalesForce] Check picklist value in apex code – just check api name or create custom metadata

In my apex class and trigger code I have If statements based on picklist values. Using picklists in code I know uses the api name of the picklist value.

My question is, is it ok to just use the api name or is it better to create custom metadata?
So if my picklist consists of 2 values for example:

Values | API Name

Old stuff  |  Old
New stuff  |  New

Is is good enough to just write the code like this:

If (act.status__c = 'Old'){ 
   act.status = 'New';
}

which is basically hard-coding, which I don't like to do, even if it's using the API name. If I create custom metadata, I still end up checking actual values though so it doesn't seem better. Is there a better way to check and set picklist values in code?

I should mention that my picklist has many values – it's the Status for Orders and there are several for the 'Draft' option as well as for the 'Activated' option.

Best Answer

We create Apex enum-style wrappers for picklist field values so that we have compile-time constants for those values as well as convenient access to the values' labels, active state, and notion of which value is the default. We included the base class for these enums in our sirono-common open source Apex class library:

https://github.com/blueprinthealth/sirono-common#apex-picklist-enums

Take a look and, if you decide to go that route, let me know if you have any questions about the approach.

Related Topic