Salesforce object describe has big data, how to get limited data like picklist values from salesforce object describe
Asked Answered
L

3

2

I am looking for way to get fields and picklists for a salesforce object. I can do it with a REAT API call using /describe after the object name. But sometimes the returned JSON data is really big with 95% extra data I don't want, with repetitive pattern strings.

This would be too inefficient to pull all that data, which could actually be as large as 2.8Mb, just to get small info I require.

How can I query query filter this data to get more specific results? Or is there a better way to get picklists for a field, or any other sub data from that big json at /describe?

Here is what I am using currently

https://[myinstance].salesforce.com/services/data/v51.0/sobjects/Casedata/describe

Lye answered 7/8, 2021 at 3:22 Comment(0)
P
2

You can query FieldDefinition table in Tooling API, for example

/services/data/v52.0/tooling/query?q=SELECT+Metadata+FROM+FieldDefinition+WHERE+EntityDefinitionId+=+'Account'+AND+QualifiedApiName+=+'Status__c'

(...)
"valueSet" : {
        "controllingField" : null,
        "restricted" : true,
        "valueSetDefinition" : {
          "sorted" : false,
          "value" : [ {
            "color" : null,
            "default" : false,
            "description" : null,
            "isActive" : null,
            "label" : "Prospect",
            "urls" : null,
            "valueName" : "Prospect"
          }, {
            "color" : null,
            "default" : false,
            "description" : null,
            "isActive" : null,
            "label" : "Live",
            "urls" : null,
            "valueName" : "Live"
          }, {
            "color" : null,
            "default" : false,
            "description" : null,
            "isActive" : null,
            "label" : "Cancelled",
            "urls" : null,
            "valueName" : "Cancelled"
          }
(...)

The picklist values will be in the Metadata field but to query it you need to ensure only 1 row is returned. So if you need 3 picklists - that's 3 API calls...

It'll return the "master" picklist, not filtered by record type.

There's also interesting table called PicklistValueInfo. It's not described too well, it's a related list to EntityParticle. You can query to get multiple picklist values in 1 go

SELECT DurableId,EntityParticleId,IsActive,Label,Value 
FROM PicklistValueInfo
WHERE EntityParticle.EntityDefinition.DeveloperName = 'Account' AND 
(DurableId LIKE 'Account.Industry%' OR DurableId LIKE 'Account.Type%')
ORDER BY DurableId

enter image description here

Or use it related list style (which might be closer to results of describe call?)

SELECT DataType, FieldDefinition.QualifiedApiName,
    (SELECT Value, Label FROM PicklistValues)
FROM EntityParticle 
WHERE EntityDefinition.QualifiedApiName ='Account'
    AND QualifiedApiName IN ('Industry', 'Type', 'Status__c')

If you use record types - UI API David linked to is easiest.

https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_resources_picklist_values_collection.htm

You can grab them all

/services/data/v52.0/ui-api/object-info/Account/picklist-values/012...

enter image description here

Or build links like shown on the screenshot to get data for single field.

Plaudit answered 7/8, 2021 at 8:23 Comment(1)
The SOQL query was especially helpful, as it gave me a very easy way to get a list of the picklist values from the Workbench.Janeyjangle
A
1

The only other API that might be applicable to your use case is the UI API, which is intended to provide the information a client would need to render the UI for a record or object. For example, the Get Object Metadata endpoint might (or might not) suit your needs. Its response body is also not particularly small.

You cannot filter describe data. You're already using the smallest-scoped version of that API.

Aviv answered 7/8, 2021 at 6:5 Comment(0)
B
0

If you are puzzled which recordTypeId to use in UI API query you can try 012000000000000AAA as specified in this sample request

Record type Id. Use 012000000000000AAA as default when there are no custom record types.

Works well in my case where I do not have Record Types created.

Brewery answered 6/7, 2022 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.