2

I have a picklist field called Meeting Type that's being used in multiple classes.

Whenever someone wants to change the value of the picklist, i have to search for all instances of that value in the code and replace them, instead of managing these values in one place.

How can i create a public set of strings that defines the different values of meeting types?

Or, is there an API Name to every picklist value that i can access via apex?

3

You can reference the field itself via its API name using this compile-time checked syntax:

// As a specific type that you can then do e.g. metadata describes calls from
SObjectField f = YourCustomObject__c.MeetingType__c;

// Just the string 'MeetingType__c' but compile-time checked
String s = String.valueOf(YourCustomObject__c.MeetingType__c);

but I think you are talking about the picklist option values themselves. Where Apex logic is dependent on those, this rather brute-force approach works:

public class MeetingType {
    // Rest of code is dependent on these variable names not the values
    // so the values can be changed in the future or dynamically loaded
    public static final String TYPE_1 = 'Type 1';
    public static final String TYPE_2 = 'Type 2';
    ...
}

with the values referenced in other code via e.g. MeetingType.TYPE_1.

Unfortunately, Apex enums are very basic in that they can't represent names containing spaces or that are not legal Apex variable names. You could use enum values if you don't mind adding supporting methods and maps (or switch statements) to do the conversions:

public class MeetingType {

    public enum Value {
       TYPE_1,
       TYPE_2,
       ...
    }

    public static String toString(Value v) {
        ...
    }

    public static Value valueOf(String s) {
        ...
    }
}

But as you comment, there isn't an elegant solution AFAIK.

| improve this answer | |
  • That's the solution i thought off, i was hoping there is a more "elegant" solution like enums with values, but seems like not. Thanks for the help! – Dana Griff Feb 2 at 12:04
  • Are any of the solutions u suggested will affect performance in any way? – Dana Griff Feb 2 at 12:15
  • Hi @DanaGriff, Any performance changes would be insignificant. Any database call is 1000's of times more expensive than simple Apex code and keep this in mind too wiki.c2.com/?PrematureOptimization. – Keith C Feb 2 at 12:30
  • Thank you very much! – Dana Griff Feb 2 at 16:45
0

You can use custom settings.

Then you define the picklist values once, and you can load it to each class when needed.

| improve this answer | |
  • A custom setting is not a good solution to save any picklist value i use a lot. i was looking for something like an ENUM, but cant seem to find the right solution. – Dana Griff Feb 2 at 11:54
  • @DanaGriff why isn't it a good solution ? – Json Feb 2 at 15:23
  • This is not the purpose of a custom setting. That's why we can define classes and enums. Imagine i have 500 picklists. Will i have 500 custom settings? Also, i will have to remember to change the custom setting everytime the value of a picklist is changed. Makes much more sense to change it in code directly. – Dana Griff Feb 2 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.