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
Arun Deepan LJArun Deepan LJ 

Custom Label in Picklist

Hi,
How custom labels can be used in the picklist. While creating a picklist field, can we directly refer the values from the Custom labels?. I know about the translation settings, but what I want, I don't want the picklist values to be hardcoded, since the picklist value is referred in Apex coding in multiple place. And Hence, a change in Picklist means, I have change every where in the code. What is the best solution to such scenario?
 
Best Answer chosen by Arun Deepan LJ
sachin kadian 5sachin kadian 5
In this case use can use the dynamic SOQL.please have a look at this link

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm

You can store the custom label value in any variable and use that in where condition as
String filterCondition = Label.filter_condition_Label;

[SELECT id, Name from custom_object__c where custom_picklit__c = : filterCondition ]


 

All Answers

sachin kadian 5sachin kadian 5
If you want to save the picklist values in custom label, you can store comma seperated and then you can use the following code in apex to make a picklist from that.
 
public class selectOptionClass {
    public List<SelectOption> selectOptions{get;set;}
    public selectOptionClass(){
        selectOptions = new List<SelectOption>();
        for(String val : Label.pick_list_values.split(',')){
            selectOptions.add(new SelectOption(val,val));
        }
 
    }
}
 
<apex:page controller="selectOptionClass">
    <apex:form>
    	<apex:selectList size="1">
        	<apex:selectOptions value="{!selectOptions}"></apex:selectOptions>
        </apex:selectList>
    </apex:form>
</apex:page>

 
Arun Deepan LJArun Deepan LJ
Hi,
Thats a quite good option. But I wanted is, Generally we use picklist values in many SOQL queries as "where" condition to retrieve the records, such [SELECT id, Name from custom_object__c where custom_picklit__c = 'Option 1' ]. In such scenario, I don't want to hardcode the value as 'Option 1'. That must admin configurable, so that, any change in that pick list value in future, does not require to edit the Apex Code.
sachin kadian 5sachin kadian 5
In this case use can use the dynamic SOQL.please have a look at this link

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm

You can store the custom label value in any variable and use that in where condition as
String filterCondition = Label.filter_condition_Label;

[SELECT id, Name from custom_object__c where custom_picklit__c = : filterCondition ]


 
This was selected as the best answer
Arun Deepan LJArun Deepan LJ
Hi Sachin ,
Thank you for your answer. I am aware of using the Dynamic SOQL. In this case, when a pick list value is changing. Need to change in two places. 
1 in pick list field itself and
2 in the custom Label

is that right
sachin kadian 5sachin kadian 5
yes Arun. You need to change 2 places for this . In latest release (Winter 16 ) salesforce introduced something new called "Global Picklist"  that is if you are using some picklist in many objects, you can create a global picklist and in every object you can get the value in its picklist from global picklist. I am not sure if you need that also or not but you can definitely have a look at that also. it will share the same picklist values across different objects .

https://help.salesforce.com/HTViewHelpDoc?id=fields_creating_global_picklists.htm&language=en_US

 
Arun Deepan LJArun Deepan LJ
Thanks for spending your precious time sachin
reatlimecoreatlimeco
Don't use a Label to compare with a picklist value (api value) in apex.  Label's are translated and are the picklist label (not the value).  Use custom settings.