[SalesForce] Fetch dependent picklist values depending on record type

I have a two picklists PL1 and PL2. PL1 is controlling picklist and PL2 is dependent picklist.

I want to fetch values of PL1 in Apex depending on recordTypes in Apex method, which I already have using the API endpoint

/ui-api/object-info/{0}/picklist-values/{1}/{2}.

Now I want to fetch the dependent values of PL2 in a map depending on record type. Is there any API endpoint for the same?

I have already read some blogs on fetching dependent picklist values, but none of them implement record types.

Best Answer

According to UI API for Salesforce:

The endpoint:

GET /ui-api/object-info/{objectApiName}/picklist-values/{recordTypeId}/{fieldApiName}

gives you valid values for record type along with controlling fields and their values.

In the documentation, it gives an example for Country to City dependent picklist for a given recordType. The sample response would look like

{
  "picklistFieldValues" : {

    "Cities__c" : {
      "controllerValues" : {
        "Australia" : 0,
        "Brazil" : 1,
        "China" : 2,
        "Colombia" : 3,
        "France" : 4,
        "Italy" : 5,
        "Mexico" : 6,
        "New Zealand" : 7,
        "Nigeria" : 8,
        "Senegal" : 9,
        "South Korea" : 10,
        "US" : 11
      },
      "defaultValue" : null,
      "url" : "/services/data/v44.0/ui-api/object-info/account/picklist-values/012000000000000AAA/Cities__c",
      "values" : [ {
        "attributes" : null,
        "label" : "Cali",
        "validFor" : [ 3 ],
        "value" : "Cali"
      }, {
        "attributes" : null,
        "label" : "Chicago",
        "validFor" : [ 11 ],
        "value" : "Chicago"
      }, {
        "attributes" : null,
        "label" : "Dakar",
        "validFor" : [ 9 ],
        "value" : "Dakar"
      }, {
        "attributes" : null,
        "label" : "Lagos",
        "validFor" : [ 8 ],
        "value" : "Lagos"
      }, {
        "attributes" : null,
        "label" : "Los Angeles",
        "validFor" : [ 11 ],
        "value" : "Los Angeles"
      }, {
        "attributes" : null,
        "label" : "Oaxaca",
        "validFor" : [ 6 ],
        "value" : "Oaxaca"
      }, {
        "attributes" : null,
        "label" : "Wellington",
        "validFor" : [ 7 ],
        "value" : "Wellington"
      } ]
    },

Which you can easily parse and create map of dependent picklist values depending on record type

Here for dependent field City__c see the controlling values for countries in controllerValues

Src: https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_features_records_dependent_picklist.htm?search_text=dependent

Related Topic