How to Deploy Custom Metadata Records

January 26, 2023

By Clay Waugh, Senior Salesforce Solution Architect

Share

What is Custom Metadata


Custom metadata is customizable, deployable, packageable, and upgradeable application metadata. First, you create a custom metadata type, which defines the form of the application metadata. Then you build reusable functionality that determines the behavior based on metadata of that type. Similar to a custom object or custom setting, a custom metadata type has a list of custom fields that represent aspects of the metadata.


Why use Custom Metadata to hold Record ID’s?


As a best practice you should avoid hardcoding Salesforce IDs because when the new record types are created in a sandbox, and then created in every sandbox leading up to and including Production, the new record’s Salesforce ID will not be the same. Therefore, you will need to update each reference of this hardcoded Salesforce record ID in every sandbox or production. This results in different versions of your formula, validation rule. process builder or flow, etc. leading to a maintenance nightmare!


Enter Custom Metadata as a single source of truth!!!


On the declarative side, we can employ the same configuration tools that programmers employ in their code, such as Custom Metadata Types to store Salesforce IDs so they can be maintained outside of a formula field, validation rule, process builder, flow, etc.(Note: Process builder is being retired).  One thing to keep in mind is that formulas and validation rules typically reference the 15 character ID and Flow will recognize the 18 character ID.


Now you won't need to adjust each and every formula, validation rule, process builder, or flow  if the 15 or 18 Character ID is referenced in multiple areas. When the ID’s differ in your other Orgs, you simply need to update the one Custom Metadata record, and every formula, validation rule, process builder, or flow will reference this one source of truth.


How to Create Custom Metadata Records for Record ID's



1. Go to Home | Salesforce




2. Click on Setup




3. Click on Setup…



4. Type "Custom Meta"



5. Click on Custom Metadata Types


6. Click on New Custom Metadata Type




7. Go to Custom Metadata Types | Salesforce


8. Name the Label "Master Record Type Table"




9. Type "Master Record Type Table"



10. Click Save





11. Go to Custom Metadata Types | Salesforce



12. Click on new_field






13. Select "Text" and "Next"...





14. Field Label "18 Character ID" and Length "18"

This will hold the 18 character record ID






15. Click Next...





16. And "Save"





17. Create these additional fields

  • Record Type API Name (255 Characters)- This should be the exact API name of your record type
  • Object API Name (255 Characters)- This will be useful if you have several record types so you can sort them in flows by object
  • 15 Character ID (15 Characters)- This will be used in validation rules



18. Create a new Record for each Record Type ID in your Org



How to use Custom Metadata in a Validation Rule



1. In the formula builder of the Validation Rule click on "Insert Field"




2. In the formula builder of the Validation Rule click on "Insert Field"


$CustomMetadata > (Whatever you named your Custom Meta Data > (Metadata Record Name) > (The field you want to reference)


In this case we are going to reference the 15 Character ID





3. This is what it will look like

Now you won't need to adjust each and every formula if the 15 Character ID is referenced in numerous validation criteria. When the IDs differ in your other Orgs, you simply need to update the one Custom Metadata record, and all of your validation rules will reference this one source of truth.






Create Subflow for Record ID's



1. Create an autolaunch flow that can be called from any other flows


2. Start with a "Get Records" element





3. Click on *Object and find the Custom Metadata Object you just created





4. Store All records





5. Click on Automatically store all fields





6. Click on Done





7. Add a "Loop" element





8. Loop through the items from the Get element




9. Use a decision element to determine each record type




10. Click on Current Item from Loop Loop_CMDT_Records > Label




11. Assign each outcome as a Variable





12. The variable should be the Custom Metadata Label





13. Click on Available for output




14. Click on Done





Now you can use this subflow in any other flow that requires you to use Record ID’s.


I hope that helps to see one of the many ways Custom Metadata can be used to store values.  This can also be done in a similar fashion if you have several queues that need to be referenced.  You can create another Custom Metadata record that stores all of your Queue ID’s and another subflow to reference when assigning records to queues in salesforce.