7

Can you please guide me best way to retrieve all active picklist values of all recordtypes of object. Can it be possible through API or any Apex Script.

I want something like below and i will write them in my file:

Obect Name, RecordTypeName, PicklistValues

Thanks

1 Answer 1

9

Unfortunately, picklist + record type information has very uneven coverage across Salesforce APIs.

You can retrieve all picklist values without record type information using either Apex or the Tooling API (a single query, in the latter case).

You can retrieve picklist values with record type information, for a single record type at a time, using the UI API, with one callout for each field or one for all fields on the object at once.

You can get all of the information about record types and dependencies by performing a retrieve using the Metadata API that includes the appropriate object and field entries.

UI API

The UI API lets you make REST requests to source picklist value details.

For example, you can send a GET request to

/services/data/v42.0/ui-api/object-info/Opportunity/picklist-values/<your record type id>

to get back a JSON bundle covering all the picklists and their legal values for that record type, or call

/services/data/v42.0/ui-api/object-info/Opportunity/picklist-values/<your record type id>/StageName

substituting the field API name you're interested in for StageName, to get back JSON for just a single field, like this:

{
  "controllerValues" : { },
  "defaultValue" : null,
  <attributes snipped>,
  "values" : [  
   {
    "attributes" : {
      "closed" : false,
      "defaultProbability" : 0.0,
      "forecastCategoryName" : "Pipeline",
      "picklistAtrributesValueType" : "OpportunityStage",
      "won" : false
    },
    "label" : "New",
    "validFor" : [ ],
    "value" : "New"
  }, {
    "attributes" : {
      "closed" : false,
      "defaultProbability" : 25.0,
      "forecastCategoryName" : "Pipeline",
      "picklistAtrributesValueType" : "OpportunityStage",
      "won" : false
    },

 ... and so on ...

See the UI API documentation for more:

Tooling API

Using the Tooling API, you can issue a query like the following:

SELECT Label, Value FROM PicklistValueInfo WHERE IsActive = true AND EntityParticleId = 'Opportunity.StageName'

In Workbench, this will yield

    Label   Value
1   Demo/Pre-proposal   Demo/Pre-proposal
2   Contract Negotiation    Contract Negotiation
3   Lead    Lead
4   Contract Execution  Contract Execution
5   Lost    Lost
6   On Hold On Hold

... etc.

Apex

In Apex, you can call getPicklistValues() on any DescribeFieldResult for a picklist-type field. Each PicklistEntry object includes an isActive() method you can call. E.g.,

Schema.DescribeFieldResult dfr = Opportunity.StageName.getDescribe();

for (PicklistEntry pe : dfr.getPicklistValues()) {
    if (pe.isActive()) {
        // do something 
    }
}

There are some clever workarounds people have developed over the years for getting record type dependency information in Apex, but none of them are supported.

Metadata API

The Metadata API is a big topic, so this will only be a quick note. When you pull down an object's metadata, you'll get back XML that includes <recordTypes> elements, which contain <picklistValues> elements:

<recordTypes>
    <fullName>Test</fullName>
    <active>true</active>
    <businessProcess>Test</businessProcess>
    <description>Test.</description>
    <label>Test</label>
    <picklistValues>
        <picklist>Changes__c</picklist>
        <values>
            <fullName>Approved</fullName>
            <default>false</default>
        </values>
        <values>
            <fullName>Requested</fullName>
            <default>false</default>
        </values>
       ... and so on...

And you'll also get <fields> entries for the object:

<fields>
    <fullName>Changes__c</fullName>
    <externalId>false</externalId>
    <label>Changes</label>
    <picklist>
        <picklistValues>
            <fullName>Approved</fullName>
            <default>false</default>
        </picklistValues>
        <picklistValues>
            <fullName>Requested</fullName>
            <default>false</default>
        </picklistValues>
        ... and so on...

You could write quite a bit of code to parse out and correlate all of that information from the object's XML source (or the return values via your favorite SOAP client/library), the details of which are going to be beyond the scope of just one answer.

3
  • Thanks a lot David. Can you please help me one more thing, Can i narrow down picklist values to 'Avialiable and Selected' as per record types. Like if account has record Type name client, so i can know if Clien't state field have which values on selected side and which are on available side Commented Jun 8, 2018 at 20:48
  • I am not aware of a way to get that information in a single query. You would need to pull the full set of picklist values and then compare with the set that is enabled for the record type.
    – David Reed
    Commented Jun 8, 2018 at 20:53
  • 1
    Because this is such a highly viewed question, I wanted to add on here that as long as you have My Domain enabled, you can actually perform the REST API call via Apex without needing to set up a Remote Site Setting. The example listed under the getOrgDomainUrl() is a great starting point: developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/… Commented Dec 1, 2021 at 23:05

You must log in to answer this question.

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