1

I am looking for a SOQL query that can display where a particular field is being referenced in Salesforce. Salesforce already has a button called "Where is this used?" for custom fields that displays all the field references. How would I write that function as a SOQL query?

2

Related to the previous post here is the sample query you can run in Developer Console with 'Use Tooling API' enabled in Query Editor.

SELECT MetadataComponentType, MetadataComponentName, RefMetadataComponentName, RefMetadataComponentId
FROM MetadataComponentDependency
WHERE RefMetadataComponentId = 'fieldId' AND RefMetadataComponentType = 'CustomField'
ORDER By RefMetadataComponentName

You need some way of getting FieldId though to filter by RefMetadataComponentId.

In SOQL you can't execute it because it doesn't support ToolingApi. In order to do so you need to do HttpRequest to Tooling API.

As an example you can use this post Tooling API usage in salesforce Apex method

Here is a link to documentation https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/intro_rest_resources.htm

Use this endpoint /services/data/${API_VERSION}/tooling/query/?q=

UPDATE thanks Jeff for the Field Query.

You can use this field to get Id for particular field

SELECT Id 
FROM CustomField 
WHERE FullName = 'Object_Api_Name__c.Field_Api_Name__c'
3
  • 1
    If you are looking for references to a Custom Field, you can use the same Tooling API to query the CustomField object to get the Id reference you'll find in the RefMetadataComponentId and MetadataComponentId fields for CustomFields: select Id from CustomField where FullName = 'Object_Api_Name__c.Field_Api_Name__c'
    – Jeff Kranz
    Mar 25 at 4:26
  • 1
    thanks, I'll update the answer
    – ytiq
    Mar 25 at 7:37
  • Thank you, this was really useful.
    – Reaper1883
    Mar 30 at 19:44
0

I believe the object you are looking to query is: MetadataComponentDependency

This is most likely what Salesforce uses behind the scenes with the 'Where is this used?' button. I have not tested this, but according to the documentation you should be able to call this using the Tooling API or Bulk API 2.0

Please note, it says that this is currently a feature in Beta so usage may be limited.

Here is a link to the Dependency API/Field Usage chatter group

If you read through the posts, you can probably get a better idea of how to query and what works / doesnt.

Hope that helps and is what you were looking for!

1
  • Thanks for the reply. You are right, Salesforce does use MetadataComponentDependency in Tooling API for this.
    – Reaper1883
    Mar 30 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.