Custom Metadata Type has become an essential ingredient when it comes to developing a configurable app in Salesforce. Many ISVs app on AppExchange is using CMT ( Custom Metadata Type ) in their apps. You may have used custom metadata types too in your app but when it comes to inserting/updating records using apex, you require some extra efforts compare to DML on sObjects. In this blog, I am going to help with that and I hope it will save your time.
Let’s understand everything with an example. We are using an online payment form and we allow an admin to change the credit card charges instead of hard-coding them in the apex code. So being a Solution Architect, I proposed a solution where we can use a Custom Metadata Type and let admin update it later on. So we have created a CMT i.e. Credit Card Charge Setting
Now, an admin can create records for Amex, Visa, Master card charges. See the above screenshot where it stats that Visa card type will be charged 2% of the amount (as credit card charges). We are going to create a lightning component which will let admin see these settings and allow them to update/insert.
We have used CustomMetadata in the code for insert/update custom metadata records. Insert/update on Custom Metadata Types is asynchronous and we will get the job id after submitting the request for deployment.
[gist id=c2124c546c1e035c4fc07cf2613192e0 file=CreditCardSetting.cmp]
In the above component, we are creating a data table using the lightning:datatable and showing all the records of custom metadata type. This data table allows a user to update a custom metadata type record. Also, a user can create a new record by clicking the New button.
CreditCardSettingController.js
[gist id=c2124c546c1e035c4fc07cf2613192e0 file=CreditCardSettingController.js]
CreditCardSettingHelper.js
[gist id=c2124c546c1e035c4fc07cf2613192e0 file=CreditCardSettingHelper.js]
CreditCardSetting.css
[gist id=c2124c546c1e035c4fc07cf2613192e0 file=CreditCardSetting.css]
CreditCardSettingController.apxc
[gist id=c2124c546c1e035c4fc07cf2613192e0 file=CreditCardSettingController.apxc]
CustomMetadataCallback.apxc
[gist id=c2124c546c1e035c4fc07cf2613192e0 file=CustomMetadataCallback.apxc]
As you can see in the example that we are setting field values to the custom metadata record and then enqueuing it for the deployment. We are defining the callback actions since this is an asynchronous job. You can have your own logic in the callback method like sending an email alert on job completion.
Here is the link for complete gist.
Lightning Component for Insert/Update Custom Metadata Type Records
By Naveen Sharma
January 10, 2019A big thank you!
The code is really nice, and works very well!
Big up to you my friend!
By Naveen Sharma
July 4, 2019if we want to delete metadata using apex, what to do ??
By Naveen Sharma
February 17, 2020Thank you! This is exactly what I was looking for. In the lightning component’s controller when defining the columns, what should “long text area” fields be defined as? This is the only field type that I cannot get to display or update correctly.