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?
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
WHERE RefMetadataComponentId = 'fieldId' AND RefMetadataComponentType = 'CustomField'
ORDER By RefMetadataComponentName
You need some way of getting FieldId though to filter by
In SOQL you can't execute it because it doesn't support
ToolingApi. In order to do so you need to do HttpRequest to
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
UPDATE thanks Jeff for the Field Query.
You can use this field to get Id for particular field
WHERE FullName = 'Object_Api_Name__c.Field_Api_Name__c'
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!