Custom Metadata Types in Salesforce are similar to Custom Objects in Salesforce. It generally allows the user to create custom sets of data that will be retrieved without the use of any SOQL, hence there is no requirement for any SOQL query to be used to fetch Custom Metadata Types. It is the advanced version of Custom Settings in Salesforce.
Salesforce Developers use Custom Metadata Types to store necessary business information and this information is static in nature, which means it will not alter by any user. The biggest advantage of the Custom Metadata type over Custom setting is the records can be deployed from Sandbox to another org by using a change set or managed package, which is not available in Custom Setting.
Creating Custom Metadata Types in Salesforce
So let’s take an example of a List of Countries to understand Custom Metadata Type.
Step 1: Create Object in Metadata Type
So let’s move to the Custom metadata type page by Home | Quick Find | Custom Metadata Type.
We need to click on the New Custom Metadata Type button. We need to fill in the required details and select the visibility of the Custom Metadata Type. After filling in all the details click on the Save button.
Custom Metadata Type objects are created with some Standard Fields. If you notice, the API name of the Custom Metadata Type is not ending with __c which is the case for Custom Object. The API name of the Custom Metadata type is Country__mdt it ends with __mdt.
Step 2: Create Field in Metadata Type
Now, we can create our own custom fields in the Metadata as we are creating fields on the Custom object. We can also create validation tule and set the Page layout of metadata type.
For creating a field, click on New next to Custom fields. You can select the data type and field type which you are willing to create, I am using Text as the data type of fields as I am storing the country names in that field.
After creating the text field, our custom metadata type field will look something like the below image.
Step 3: Create a Record in Metadata Type
Now, we need to create the record in the newly created metadata type object. For creating record, we need to click on the Manage Countries button at the top of the page.
Then we need to click on the New button, and an interface will open in which we need to country label, country name, and other details as mentioned in the below image, and then click on the Save button.
A new record is created in country metadata, Similarly, you can create multiple records in this metadata.
Step 4: Let’s Use Newly Created Metadata
If you want all the records which are created metadata, you can simply use the below code to get all the data.
System.debug('Custom Meta Data'+Country__mdt.getAll());
If you want only a single record then you can use the below code.
System.debug('Custom deta'+Country__mdt.getinstance('India'));
At last, if you want to deploy these settings from one org to another org, you can simply use the change set to pass these settings without any rework on the destination org.
Congratulations!
You’ve successfully created Custom Metadata Type in Salesforce.
I hope you like this tutorial and if you want any help let me know in the comment section.
Stay tuned, there is way more to come! follow me on Youtube, Instagram, Twitter, and LinkedIn. So you don’t miss out on all future Articles and Video tutorials.
. . .
I am delighted that you read my article! If you have any suggestions do let me know! I’d love to hear from you. ????
About the Author
Shubham Agarwal is a passionate and technical-driven professional with 5+ years of experience in multiple domains like Salesforce, and iOS Mobile Application Development. He also provides training in both domains, So if you looking for someone to provide you with a basic to advance understanding of any of the domains feel free to contact him
Pingback: Custom Setting in Salesforce - Let Create An App