22

I am using SOQL to query the opportunity. When I use the query,

SELECT CUSTOM_PICK_LIST_COL__C FROM OPPORTUNITY

I am getting the values under the API Name of the picklist items. How to get the value from the Values of the picklist? enter image description here

3
  • Can you give any example exactly what you want? Commented Mar 2, 2017 at 12:09
  • I don't think so you can do that using SOQL query. Instead you can do that using Apex
    – RCS
    Commented Mar 2, 2017 at 12:24
  • What are these Action, Values and API Name columns?
    – Saroj Bera
    Commented Mar 2, 2017 at 12:29

2 Answers 2

74

You can use toLabel() to convert the picklist api name to a label:

SELECT toLabel(CUSTOM_PICK_LIST_COL__C) FROM OPPORTUNITY

2
  • 1
    If you already have the field, you can use toLabel(CUSTOM_PICK_LIST_COL__C) direcly on the code, don´t need the soql query. Commented Feb 22, 2022 at 14:08
  • 1
    If you need both you can alias the label : SELECT toLabel(CUSTOM_PICK_LIST_COL__C) fieldLabel, CUSTOM_PICK_LIST_COL__C from Opportunity Commented Oct 13, 2023 at 9:12
10

You don't need to use SOQL to do this, instead you can get it using Apex's Schema utility and DescribeFieldResult class. For example:

public List<String> myPicklist {
  get {
    if (myPicklist == null) {

      myPicklist = new List<String>();
      Schema.DescribeFieldResult field = Object__c.MyPicklist__c.getDescribe();

      for (Schema.PicklistEntry f : field.getPicklistValues())
        myPicklist.add(f.getLabel());

    }

    return myPicklist;          
  }
  set;
}

This just returns a List<String> of labels of the field MyPicklist__c in object Object__c.

You might want to review what you can do with that class here.

Bonus: you don't use any SOQL and contribute to limits.

3
  • 4
    This is useful for getting ALL of the possible picklist values for a field, but his specific question is about getting the label for a particular picklist value back from SOQL instead of the api name. Commented Mar 2, 2017 at 12:58
  • @JohnTowers That's true and your response definitely answers the question directly, but it's a bit of an XY problem. Yes you can use SOQL to get Picklist values, but that doesn't mean that you should. It's nice to put both answers on the table though. :) +1!
    – Dan Jones
    Commented Mar 2, 2017 at 13:00
  • 5
    These are two different use cases, though. If I want a list of all possible values for the field, your answer is the correct answer. But this question is about querying a record and returning the picklist value's label instead of it's API name. As of Spring '17, when you select a picklist field from a record the picklist value's API name is returned. You have to use toLabel() to retrieve the picklist value's label instead of it's API name in your results. Commented Mar 2, 2017 at 13:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .