Salesforce: Querying Tooling API

Recently I have found interesting question about FieldDefinition Metadata field on SSE.
Some person was trying to query for it in usual apex. Salesforce documentation and behavior might be misleading, since they have removed some old good documentation and there are many features which are not documented, poorly documented or which documentation cannot be found.

At least I couldn’t find any mentions in Salesforce documentation that EntityDefinition and FieldDefinition objects are available for usual Apex SOQL query.

There was once in the past list of all Salesforce Standard Objects in salesforce documentation, now it is gone. There is only list of Standard Objects available in SOAP API but this is not what we are looking for.

When some user is trying to run some query against EntityDefinition and FieldDefinition object and gets successful results without querying for Metadata field and gets error if he or she is trying to query Metadata field this might be very confusing and misleading.

If we run a query select id, DataType,NamespacePrefix, DeveloperName from FieldDefinition where EntityDefinition.QualifiedApiName = 'Account' AND DurableId ='Account.Industry' in Developer Console Query Editor even without having checked Tooling API checkbox or if we run this query in Apex Code we will receive results without error.

rschr

If we run a query select id, DataType,NamespacePrefix, DeveloperName, Metadata from FieldDefinition where EntityDefinition.QualifiedApiName = 'Account' AND DurableId ='Account.Industry' in Developer Console Query Editor without having checked Tooling APIcheckbox or if we run this query in Apex Code we will obtain an error

hwexc

Please notice that Tooling API checkbox is unchecked.

If we run a query select id, DataType,NamespacePrefix, DeveloperName, Metadata from FieldDefinition where EntityDefinition.QualifiedApiName = 'Account' AND DurableId ='Account.Industry' in Developer Console Query Editor having checked Tooling API checkbox or if we run this query through callout to Tooling API we will receive results without error.

w3sny

Which proves my statements above that:

  1. Standard objects FieldDefinition and ObjectDefinition are available by usual SOQL queries from usual Apex code except for Metadata field.
  2. Field Metadata on FieldDefinition is available only by Tooling API calls.

However in my humble opinion this is useless to run this query in Developer console query editor since Developer Console will not show us any data but only [Object object] string which is not really helpful.

So to get that information in Apex you really need to make self-callout.

Before doing that you need to go to Remote Site Settings by link https://login.salesforce.com/0rp/e?SiteName=self&EndpointUrl=https://yourinstance.salesforce.com/ on production or Developer Edition instance or https://test.salesforce.com/0rp/e?SiteName=self&EndpointUrl=https://yourinstance.salesforce.com/ on any of sandbox instance and input your data and click save like this is done on this screenshot

IyY0u.png

Then you can run the following code to get the field metadata

static String restGet(String endPoint, String method, String sid) {
Http h = new Http();
HttpRequest hr = new HttpRequest();
hr.setHeader('Authorization', 'Bearer ' + sid);
hr.setTimeout(60000);
hr.setEndpoint(endPoint);
hr.setMethod(method);
HttpResponse r = h.send(hr);
return r.getBody();
}

String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
//restGet( baseURL, 'GET', UserInfo.getSessionId() );
String body = restGet( baseURL +
'/services/data/v38.0/tooling/query?'+
'q=select+id,+DataType,NamespacePrefix,+DeveloperName,+Metadata+'+
'from+FieldDefinition+'+
'where+EntityDefinition.QualifiedApiName+=+\'Account\'+'+
'AND+DurableId+=\'Account.Industry\'',
'GET', UserInfo.getSessionId() );
System.debug(LoggingLevel.ERROR, '@@@ v: ' + body );
String metadata = body.substringBetween('"Metadata":{"', '}}]}');
System.debug(LoggingLevel.ERROR, '@@@ v: ' + metadata );

Finally we can get what we came for

log.jpg

However, if you really need metadata you could just run Apex Field Describe methods because this might be more easy than accessing FieldDefinition object Metadata field through callout.

Enjoy and hit like if we consider this post super awesome!

 

 

 

 

Advertisements
This entry was posted in apex, field, salesforce, Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s