3

I have a picklist field say Partner with one of the value as Salesforce (Label) and API name is SFDC. When a record is created with this picklist value and queried then it would return the API name (here its SFDC) but would like to read the label thru Apex code, how is it possible? I do not want to use any formula field that return the label value or use toLabel in SOQL. Thanks.

  • 1
    Did you try using apex:outputField/apex:inputField? That should automatically translate for you. – sfdcfox Jan 9 '18 at 18:56
  • @sfdcfox...I am not using VF page, I want it in apex code. Thanks. – Mahesh Raghuraman Jan 9 '18 at 19:54
  • 1
    Why do you not want to use the tools that the system gives you? I can understand not wanting to deal with a formula field. Using toLabel would seem to be quite a good way to do it, though. – DavidSchach Jan 9 '18 at 20:08
  • @DavidSchach....I am getting the records from trigger, I do not want to fire another SOQL just to read the picklist Label value. More info, Partner pick list value has partner name as Label and SSO as API value and when I want to send email to them then I want to use Label (to address them) instead of SSO. Thanks. – Mahesh Raghuraman Jan 9 '18 at 20:19
  • Fire the SOQL. It's the best-practice, and there's nothing wrong with doing a query in a trigger. Do the query. Nobody is going to give you advice that doesn't involve a query somewhere. – DavidSchach Jan 9 '18 at 20:27
5

You can get at the picklist value labels by exploring the "describes" information for your given object.

// Schema.SObjectType.Account gets you the SObjectDescribeResult
// From there, fields.Name gets you the fieldDescribeResult
// fieldDescribeResult provides a getPicklistValues() method
List<Schema.PicklistEntry> picklistEntries = Schema.SObjectType.Opportunity.fields.StageName.getPicklistValues();

for(Schema.PicklistEntry ple :picklistEntries){
    // Schema.PicklistEntry provides getLabel(), getValue(), and a few other methods
    system.debug(ple.getLabel());
    system.debug(ple.getValue());
}

The documentation on Schema.PicklistEntry provides a little more detail.

In my example, you could use Opportunity.StageName.getDescribe() instead of Schema.SObjectType.Opportunity.fields.StageName, but other than being shorter to type, I'm not sure of the pros/cons to each approach.

Of course, the above only works if you know both the object and the API name of the picklist field at compile time. If you need something a little more dynamic...

String sobjName = 'Opportunity';
String objField = 'StageName';

List<Schema.DescribeSObjectResult> describeResults = Schema.describeSObjects(new List<String>{sobjName});
system.debug(describeResults[0].fields.getMap().get(objField).getDescribe().getPicklistValues());

The furthest back I could confirm that these will work is v32.0 (Winter '15)

| improve this answer | |
0

+1 to the previous answer. You should be able to access labels and API names of individual picklist values by using getValue() and getLabel() functions.

You can find more info about Picklist Value API Names in this blog post: https://developer.salesforce.com/blogs/developer-relations/2017/01/keeping-picklist-integrations-safe-using-api-names.html

| improve this answer | |
  • How can we get the label if our managed package add a new option for a picklist in a new package version that our customer's old version doesn't have this option? – Jair Apr 9 '18 at 10:05
  • Unfortunately, picklist values are completely subscriber-controllable and if package is already installed, you can't add/update values with a package upgrade. – Vladimir Gerasimov Apr 9 '18 at 16:59
  • So, we have to hard code new option in our code? But our package supports multi-languages that is very bad way to maintain our code. – Jair Apr 9 '18 at 17:43

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.