We have integrated salesforce with our internal portal. On opportunity we have a field names Tags and there are certain values in it. When i use select Tags__c from Opportunity, i am getting only api names. How do i get the list of all labels in the Tags field? Is there a way to access the master table of the custom fields.

How to get the api name and label name in a single query?

Thank you in advance for the help

  • Tags__c is a picklist field? If so, do you want to get the labels of the picklist field instead of its API names? – derroman Aug 11 '20 at 7:03
  • Yes i want the list of labels associated with the custom field tags – Nandy Aug 11 '20 at 8:09
  • then take a look to @sfdcfox's answer below – derroman Aug 11 '20 at 8:11
  • i need to retrieve both api name and label in the same query. – Nandy Aug 11 '20 at 9:02
  • in the same query? Not possible. What's your purpose? What do you want to achieve? – derroman Aug 11 '20 at 12:27

If you need to be able to read and write the proper values, you need a describe object call (describeSObject). This is available in SOAP, REST, and the UI-API. For example, you could call /services/v48.0/sobjects/Opportunity/describe to get the describe information.

In any case, you'll get the field and all possible API values, and their respective labels to present to a user. If you just need a translated value directly from a query (read-only access), you can use SOQL's toLabel method:

SELECT toLabel(Tags__c) FROM Opportunity
  • Thank you for the help. I have one more thing here. How do i get both api_name and labels in the same query? – Nandy Aug 11 '20 at 8:10
  • @Nandy In a query, you cannot. You would need two queries. If you use the User Interface API, you can get both values at once with the Records resource, otherwise you need a describe call. – sfdcfox Aug 11 '20 at 14:35

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.