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.
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.
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.
I am certified Salesforce Developer with Sales and Service cloud certifications. I am a true believer in #GivingBack principle and a Salesforce enthusiast. I am working as a member of our #SalesforceOhana to learn and help other fellow Trailblazers of the community to grow and learn.
“I’m a true believer in karma. You get what you give, whether it’s bad or good.”