function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aiden ByrneAiden Byrne 

Saving enum values in Salesforce

For selected fields I want to save the enum display type in custom settings. Its a long story but for performance reasons I don't want to requery the underyling schema for these fields every time.

 

A conservative approach to saving an enum would be to save it's string value. Unfortunately unlike Java, APEX doesn't appear to offer a enum.valueOf(s) type method letting me de-reference the string parameter.
I could save the ordinal. Happily, I can easily convert ordinal back to the enum via enum.values()[ordinalNumber]
The latter approach minimizes the custom settings field size and should be fast.
Here's the thing... is it safe? If Salesforce engineering inadvertantly changes the order of enum values I'm in trouble. That said, absent the ability to easily do this another way, I would hope that Saleforce engineering is careful to view the enum value order as part of the API contract - and not change the order from release to release. Is that a safe assumption? 

 

Ritesh AswaneyRitesh Aswaney
Not sure I follow your post completely, but worth saying that you don't need a soql query to access a custom setting.
CustomSetting.getInstance(Name)
eyewellseeyewellse

Aiden said "Unfortunately unlike Java, APEX doesn't appear to offer a enum.valueOf(s) type method letting me de-reference the string parameter."

 

Actually, it does from what I can tell (perhaps it did not at the time of your post).

 

the "name()" method will yield the string of the enum value.

 

Here are more enum methods:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_enum.htm