MetadataComponentDependency

If you are a Salesforce developer, you might already know that dependencies between metadata components can be a key factor in the functionality and performance of your implementation. That’s where Salesforce’s Dependency API comes in.

But what exactly is the Dependency API and how does it work? And how about the MetadataComponentDependency? Let’s take a closer look.

What are the MetadataComponentDependency and the Dependency API in Salesforce?

First of all, let’s define these concepts in order to make the reading of this article more pleasant and understandable.

The MetadataComponentDependency object is a specific type of object in the Dependency API that represents a dependency between two components in Salesforce, containing information about the dependent component, the component it depends on, and the type of dependency. That can be, for instance, “extends”, “references”, etc.

The Dependency API is a part of the Metadata API that allows developers to track dependencies between different components in Salesforce. For instance, if a custom object depends on a custom field, the Dependency API can help track this relationship and ensure that the custom field is not deleted or modified in a way that would break the custom object.

After giving both a quick rundown, let’s discuss their benefits and drawbacks.

Advantages

By using the Dependency API, developers can view and manage dependencies in a number of ways, including:

  • Viewing metadata dependencies: The Dependency API allows developers to view the dependencies and dependents for a particular metadata component. This can help them understand how changes to a component might impact other parts of the organization.
  • Validating metadata changes: Before making changes to metadata components in Salesforce, developers can use the Dependency API to validate those changes and ensure that they will not break any existing dependencies.
  • Managing metadata changes: If developers need to make changes to a metadata component that has dependencies, they can use the Dependency API to manage those changes and ensure that they do not break any existing dependencies.
  • Identifying metadata dependencies: The Dependency API can be used to identify dependencies between metadata components that are not immediately obvious. This can be particularly useful when working with complex implementations or when making changes to existing components.

Limitations

As we know, the MetadataComponentDependency object in Salesforce represents a dependency between two metadata components. It may be used by the Dependency API to manage dependencies between different components within your Salesforce organization.

It is worth noting that there are a few limitations to keep in mind when using the MetadataComponentDependency object and Dependency API:

  • Only certain types of metadata components can have dependencies. Currently, the following types of metadata components are supported: ApexClass, ApexComponent, ApexPage, ApexTrigger, AuraDefinitionBundle, CustomObject, CustomField, CustomTab, CustomPermission, and CustomApplication.
  • You can only create dependencies between metadata components within the same namespace. If you need to create dependencies between metadata components in different namespaces, you will need to use the Metadata Service API or the Tooling API.
  • The Dependency API only supports dependencies between components that are of the same type. For example, you can create a dependency between two Apex classes, but you cannot create a dependency between an Apex class and a custom object.
  • The Dependency API does not support circular dependencies. This means that you cannot create a dependency between two components where each component depends on the other.
  • The Dependency API does not automatically propagate changes made to one component to other components that depend on it. You will need to manually update any dependent components if you make changes to the component they depend on.
  • The Dependency API does not support dependencies between components in different orgs. If you need to manage dependencies between components in different Salesforce organizations, you will need to use a different tool or approach.

Additionally, we may find other limitations if using the MetadataComponentDependency with other APIs – Tooling API and Bulk API 2.0.

  • Regarding the Tooling API, it is important to keep in mind that there cannot be more than 2000 records in a single query and that reports are not included in those MetadataComponentDependency queries. You may use Bulk API 2.0 for dependency relationships that include reports.
  • In the case of Bulk API 2.0, there can’t be more than 10,000 records per query.
  • Concerning unsupported SOQL queries in the Tooling and Bulk API 2.0, check the references section.

A note aside, Salesforce has the following note in their developer blog post regarding the availability of MetadataComponentDependency: “As a Beta feature, MetadataComponentDependency (Dependency API) is a preview and isn’t part of the “Services” under your Main Services Agreement with Salesforce. Use this feature at your sole discretion […]. Salesforce doesn’t guarantee general availability of this feature within any particular time frame or at all, and we can discontinue it at any time. […]”

Why is the MetadataComponentDependency (and Dependency API) important in Salesforce?

Understanding and managing dependencies is critical in Salesforce, as it can impact the functionality and performance of your implementation. If you make changes to a metadata component that is depended upon by other components, those changes could potentially break the functionality of the other components. For example, if you delete a custom field that is used in a report, the report will no longer be able to run properly.

By using the Dependency API, you can ensure that changes to metadata components are made in a controlled and safe manner. This can help prevent issues with the functionality of your Salesforce implementation and ensure that your organization is running smoothly.

In essence, the Dependency API and the MetadataComponentDependency object are important because they offer a mechanism to handle dependencies across various metadata components inside your business. Large, complex organizations with a variety of interdependent components may find this to be particularly helpful.

How to use the MetadataComponentDependency (Dependency API)

In order to use the MetadataComponentDependency object and the Dependency API, you will need to make API calls to its endpoint using the Salesforce REST API or SOAP API.

Example #1

First of all, make sure that you have the necessary permissions to access the Dependency API. This may require contacting your Salesforce administrator or consulting the appropriate Salesforce documentation.

Next, determine the IDs of the two components that you want to create a dependency between. You can find the IDs of your metadata components using the Metadata API or the Tooling API. In the next step, we will be using the Tooling API.

Use the following API call to create a new dependency between the two components:

POST/services/data/vXX.X/tooling/sobjects/MetadataComponentDependency/code>

In the request body, include the following parameters:

{
	“MetadataComponentId”: “component1Id”,
        “MetadataComponentDependencyId”: “component2Id”
}

In here, replace “component1Id” and “component2Id” with the IDs of the two components you want to create a dependency between, and the vXX.X with the current version of Salesforce you’re using.

Now, send the API request and wait for the response. If the request is successful, you will receive a response with the ID of the new dependency.

Example #2

To retrieve a list of dependencies for an existing dependency using the MetadataComponentDependencies, use the following API call:

GET /services/data/vXX.X/tooling/query/?q=SELECT+Id,MetadataComponentId,MetadataComponentDependencyId+FROM+MetadataComponentDependencyId+WHERE+MetadataComponentId=’componentId’

This will return a list of all dependencies for the specified component, along with the IDs of the components that the specified component depends on. An example of the response you might receive when using this API call would be the following:

{
	“records”: [
	{
		“Id”: “dependency1Id”,
		“MetadataComponentId”: “componentId”,
		“MetadataComponentDependencyId”: “dependentComponentId1”
	},
	{
		“Id”: “dependency2Id”,
		“MetadataComponentId”: “componentId”,
		“MetadataComponentDependencyId”: “dependentComponentId2”
	},
	…
 ]
}

This response shows that the specified component has two dependencies: one with the component with ID “dependentComponentId1”, and another with the component with ID “dependentComponentId2”.

Note that in order to make this request, you will need to authenticate and have the necessary permissions to access the MetadataDependencyComponent and Dependency API.

There are many other API calls and parameters available that you can use to perform a variety of actions with the Dependency API, such as updating the details of an existing dependency or deleting a dependency (CRUD, anyone?). You can find more information on these API calls and parameters in the Salesforce official documentation.

Example #3

Now for an example with real data. Similar to what we showcased in the example above, here is how you might use the MetadataComponentDependency object and the DependencyService class to retrieve a list of dependencies between the custom fields called Invoice and Invoice Line Item.


// Create the DependencyService
DependencyService ds = new DependencyService();

// Retrieve the dependencies
MetadataComponentDependency[] dependencies = ds.list('CustomField', new String[] { 'Invoice Line Item' });

// Iterate through the dependencies
for (MetadataComponentDependency dependency : dependencies) {
    // Check if the dependency is a master-detail relationship
    if (dependency.getType() == 'Extends') {
        // Print the dependent and master components
        System.out.println('Dependent component: ' + dependency.getDependentComponent().getFullName());
        System.out.println('Master component: ' + dependency.getMasterComponent().getFullName());
    }
}

This code would retrieve all dependencies for the Invoice Line Item field and then iterate through them to check if any of them are master-detail relationships. If it finds one, it will print the full names of the dependent and master components, in the following way:

Dependent component: Invoice Line Item
Master component: Invoice

Summary

In conclusion, Salesforce’s metadata component dependency and dependency API play a vital role in the organization and management of customizations within the Salesforce platform. The MetadataComponentDependency feature allows users to track and visualize the relationships between different components, while the dependency API allows developers to programmatically retrieve and manage dependency information. These tools provide a powerful and flexible way to understand and manage the complexity of customizations within Salesforce, ultimately helping to ensure the stability and reliability of the platform.

References

https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_metadatacomponentdependency.htm

https://www.slideshare.net/DeveloperForce1/demystify-metadata-relationships-with-the-dependency-api

https://forcepanda.wordpress.com/2019/11/13/dependency-api-find-all-the-referenced-metadata/

No Comments

Sorry, the comment form is closed at this time.