function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
yusuke nakayamayusuke nakayama 

How to get several field's metadata at once?

Hello Salesforce experts and users.

I would like to get fields' metadata (each field's label, qualified api name, description, and default value).

As far as I find out, I think there are two ways to do that.

1. Use SOQL (querying FieldDefinition)
https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_fielddefinition.htm
 
2. ​​​​​​​Use REST API (SObject describe())
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_describe.htm

I have tried to use these and have a problem for each methods.

The problem for SOQL is that i haven't figured out how to retrieve default value.  The SOQL queries I used are below
 
Select Label, QualifiedApiName, Description from FieldDefinition where EntityDefinition.QualifiedApiName = 'Account'; -- works fine

Select Label, QualifiedApiName, Description, defaultValue__c from FieldDefinition where EntityDefinition.QualifiedApiName = 'Account'; -- not works

And the problem for REST API is that it seems it doesn't return description in its response.
I used Python library called simple-salesforce for making an describe() API call.
https://github.com/simple-salesforce/simple-salesforce


Using both ways and combined these response would be one of the solution, but I feel it is redundant and want to avoid using it.
That is why I ask for help in this forum.

Any advice, or tips is much appreciated.

Thank you
SwethaSwetha (Salesforce Developers) 
HI Yusuke,
Using SOQL, can you confirm if the picklist's inactive values are also retrieved?Thanks
yusuke nakayamayusuke nakayama

Hi Swetha,

Thank you for your reply.

Is the field name of picklist's inactive values 'picklist' in SOQL?

If so, I can't retrieved...(If not, could you tell me the field name in SOQL?)

SwethaSwetha (Salesforce Developers) 
Hi Yusuke,
I am referring to something like the "status" field on "Account" Object which does not show if the picklist field is inactive. In regards to fetching the "defaultvalue", as per the post  (https://salesforce.stackexchange.com/questions/275539/querying-metadata-of-an-entity-via-tooling-api

defaultvalue is an attribute of CustomField Metadata on FieldDefinition entity. The Metadata field is of type CustomField and this field type has your attribute. To pull the Metadata field via a Tooling API query on an entity, you have to limit the query to return exactly 1 record in your result set.

Tooling REST API resource:
/services/data/v46.0/tooling/query?q=SELECT+Label,DurableId,EntityDefinitionId,Metadata+FROM+FieldDefinition+WHERE+DurableId='Account.Id'

Let me know if this helps.Thanks
SwethaSwetha (Salesforce Developers) 
Hi Yusuke,
Can you log a case with the salesforce support and share the case number here so I can pick and investigate further.

Thank you
Swetha Maddali
SwethaSwetha (Salesforce Developers) 
HI Yusuke,
Can you try with something like below? It worked in my Org and I was able to fetch the defaultvalue as well as description for my picklist field

/services/data/v46.0/tooling/query?q=SELECT+Label,DurableId,EntityDefinitionId,Metadata+FROM+FieldDefinition+WHERE+DurableId='01I6F000002A7iN.00N6F00000XoUhV'

Here 01I6F000002A7iN is object Id and 00N6F00000XoUhV is my custom field(picklist datatype)

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
yusuke nakayamayusuke nakayama
Hi Swetha,

Thank you very much for your support and advice.
And I am sorry for the late reply.

As you suggested, I queried 

/services/data/v46.0/tooling/query?q=SELECT+Label,DurableId,EntityDefinitionId,Metadata+FROM+FieldDefinition+WHERE+DurableId='01I6F000002A7iN.00N6F00000XoUhV'

and it says 'No such column 'Metadata' on entity 'FieldDefinition'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.'

The user I used for quering is not granted enough to access?

Thank you.
SwethaSwetha (Salesforce Developers) 
Thanks for getting back on this, Yusuke.
The IDs in the query are unique and specific to my dev org wherein 01I6F000002A7iN corresponds to a customObject and 00N6F00000XoUhV  corresponds to my org's custom picklist field.

You will need to change these ID's accordingly as per your org.Happy to answer any follow up queries.Thanks