Simplify Your Salesforce Data with Custom Metadata Type Relationships
Custom metadata types are better than ever before! In Spring ’17 we added new custom metadata relationships, which let you look up other custom metadata types, entity definitions, and field definitions. Out of the box you can create relationship between your custom metadata type and the following metadata types:
- Another custom metadata type
- Entity definition
- Field definition
Let’s look more closely at the different relationships that custom metadata types have to offer.
Relationships Between Two Custom Metadata Types
Let’s say your application needs to calculate sales tax for the various locations of your business. Usually sales tax combines both city and the state taxes. Let’s see how we can solve this use case with custom metadata relationships.
Relationship between two custom metadata types
In the example, we use two custom metadata types to store city and state taxes. Custom metadata type object for city tax has a relationship to the one that stores the state’s tax rate. This relationship makes it easy to calculate your final sales tax by combining city and state sales tax rates.
Relationships Between Custom Metadata Type and Entity Definition
Another type of relationship supported on custom metadata types is the custom metadata type to entity definition object relationship. What that means is that custom metadata types can now lookup standard and custom entities that are part of your org.
Custom Metadata Type to Entity Definition relationship
In the example above, we use Custom Metadata Type to store Service Level Agreement (SLA) data for different types of standard and custom objects that can be created in your org. Let’s say your company has a policy that newly created invoices have to be reviewed within a 12-hour time frame. So in this case, Custom Metadata Type to Entity Definition relationship helps to define SLA requirements in your org.
Relationships Between Custom Metadata Type and Field Definition
We aren’t done yet! In Spring ’17, we also introduced a new type of relationship: custom metadata type to field definition. The custom metadata type to entity and field definition relationships let you specify what particular field you want to update on each entity type.
Custom Metadata Type to Field Definition relationships
In the example above, when a new referral is created in your org, you can use your app code to populate Source__c field with a value “Internal” by using info stored in your custom metadata types.
Let’s take a look at how custom metadata type relationships can be queried in SOQL. In my developer edition org, I’ve implemented a custom metadata type with FieldDefinition relationship from the example we looked at earlier.
Custom Metadata type example
To query default value for the Account entity, developers can use a simple query like this one:
SELECT Entity_Relationship__c, Field_Relationship__c, Default__c FROM Source_Default_Value__mdt WHERE Entity_Relationship__c = 'Account'
the result looks like the following:
Custom Metadata Types vs. Custom Settings
In addition to what we described above, there are other advantages of using custom metadata types.
Custom Settings and Custom Metadata Type side-by-side
As you can see, custom metadata types provide you with significantly greater flexibility over custom settings!
Custom metadata type relationships provides additional flexibility on how you can approach your app configuration problems. With features that enable you to map custom metadata to other custom metadata, entity or field definitions, we’re making it even easier for you to configure and manage your orgs. Want to dive deeper? Check out our Trailhead Module, official docs and share your experience in the Success Community group!
About the author
Vladimir Gerasimov is a former engineer from the team that delivered Global Picklists. He has recently switched to product management and is planning to use his deep technical expertise to make life easier for Salesforce admins and developers.