salesforce data migration services

Building A Metadata Dependency Matrix Using Dependency API

In this blog – ‘Building A Metadata Dependency Matrix Using Dependency API’, We are going to explore how to use MetadataComponentDependency (Beta version released in Salesforce Winter’20 version) to analyze the dependencies of custom-built components and prepare the dependency matrix for it.

While performing an Org health check, sometimes we end up observing that there are a lot many unwanted custom development done in the past several years which are no more needed now but it becomes too difficult to delete them as we need to check their dependencies and avoid any risk while cleaning them. At that time, building a dependency matrix could be an awesome approach to this problem. So I am going to walk you through to below steps to achieve this solution :

  • Introduction to MetadataComponentDependency
  • Exploring MetadataComponentDependency Queries
  • Exporting the records retrieved via MetadataComponentDependency Queries
  • Converting JSON data into CSV/Excel format
  • Organizing the Spreadsheet data

Introduction to MetadataComponentDependency

Salesforce introduced Where is this Used? feature that displays a button on custom components. Using this button, You can see where a field is used and where changes to the field appear before you edit it. Similarly, you can leverage MetadataComponentDependency Queries feature to programmatically query the dependency metadata.

MetadataComponentDependency is still in Beta version and is not generally available. It represents dependency relationships between the metadata components in your org and is available in API version 43.0 and later.

Exploring MetadataComponentDependency Queries

To begin with, read this Salesforce article so that you have an idea about this Tooling API object and below fields :

  • MetadataComponentId
  • MetadataComponentName
  • MetadataComponentType
  • RefMetadataComponentId
  • RefMetadataComponentName
  • RefMetadataComponentType

Since now you can query Tooling API objects in the developer console, you can use the above fields and query the fields. Make sure you select the checkbox – ‘Use Tooling API’ and use the below query as mentioned in the below screenshot.

[php]

Select MetadataComponentId,MetadataComponentName,MetadataComponentType,
RefMetadataComponentId, RefMetadataComponentName,
RefMetadataComponentType from MetadataComponentDependency
limit 2000

[/php]

Once you query the fields using the SOQL query for Tooling API, you will get the required data.

You can also filter this query for most of the Tooling API objects like CustomObjects, ApexClass, etc. like below :

[php]

Select MetadataComponentId,MetadataComponentName,
MetadataComponentType,RefMetadataComponentId,
RefMetadataComponentName,RefMetadataComponentType
from MetadataComponentDependency
where MetadataComponentType = ‘ApexClass’

[/php]

You can use such filters to get the dependency table for different types like the custom field, custom object, etc.

Exporting the records retrieved via MetadataComponentDependency Queries

You can get the records in table format in the developer console but there is no easy way to export the data into excel or any other format. Few of the common hacks are mentioned in this StackExchange URL – How to export results Developer Console Query Editor. If you are looking for an easier way to just copy or extract the data you can use recommended plugins or ‘Inspect Element’ feature to do it!

Another way of doing the same is to use the Workbench tool. If you are new to the tool, it is pretty straightforward and can be used right away, You can get your hands dirty on this tool using this link. Once you login to the tool, you can Go to Utilities > Rest Explorer, and use the following URL :

[php]

/services/data/v47.0/tooling/query?q=Select%20MetadataComponentId%2CMetadataComponentName

%2CMetadataComponentType%2CRefMetadataComponentId

%2CRefMetadataComponentName%2CRefMetadataComponentType

%20from%20MetadataComponentDependency

[/php]

You need to follow a simple rule for the above URL – Use ‘%20’ for any spaces in Query and ‘%2C’ for any commas in the query.

and follow below 4 steps as mentioned in the below screenshot :

  1. Go to Utilities -> Rest Explorer
  2. Select the HTTP method GET, Please note this object supports only the GET method for obvious reasons.
  3. Copy and paste the URL prepared above and hit execute then click on ‘Show Raw Response’
  4. You will receive the JSON Raw response which you can tweak to convert the data format.
  5. You can do this similar exercise for every filter you use for metadata components

Converting JSON data into CSV/Excel format

There are many tools which do it and you can, in fact, parse the JSON in Salesforce as well. To keep it short, We are referring this JSON to CSV tool, where you can just copy the tweaked Raw response (you just need to copy the JSON and leave the rest few lines in the beginning ) and download the excel format. You can arrange the data column by deleting unwanted ones and rearranging columns of the spreadsheet.

Organizing the Spreadsheet data

So now you have data in the excel, so depending on your choice, you can either simply create one sheet per custom metadata, for example, a sheet for a custom object, apex class, and visualforce page or use the filters in the same sheet. If you are interested in building a matrix and are aware of using pivot, you can follow these steps mentioned in the video – Converting table data into matrix sample and that’s it!

Feature Limitations

  • The queried data limit is 2000 so you need to make sure you add few filters for heavily configured Salesforce Instance.
  • Check out SOQL limitations
  • Check out SOSL Limitations

Next Steps :

If you want to explore more about this feature, you can follow the reference links and do join this chatter group on Trailblazer Community. Check out these awesome blogs too :

I hope you enjoyed making your hands dirty with this AWESOME BETA feature and have understood how easy it is to build a matrix report now for org analysis, org health check, maintenance and for documentation purpose

References :