Update Custom Metadata Types using Metadata Api
With the introduction of Custom Metadata Types, the use of Custom Settings have been marginally reduced. Custom Metadata Types gives more flexibility in context of creating/maintaining Metadata, however when it comes to the point of updating Custom Metadata Types it involves some manual steps to be done from the Admin side which is a bit pain!
With the help of Metadata Api this process can be made more simple just by writing some Apex. Lets go ahead and put some code to make use of Metadata Api and update Custom Metadata Types.
Step 1: Create a new Custom Metadata Type "US_States" with one custom field "State__c".
Step 2: Create few records for the above Custom Metadata Type using "Manage" button.
Step 3: When we say that we update Custom Metadata Types using Metadata Api, behind the scenes it does an actual deployment and we use below class to track those deployment results. Create an Apex class "CustomMetadataCallback.apxc" using below code.
Step 4: Create a Visualforce Page "UpdateCustomMetadata.vfp" with an extension "UpdateCustomMetadataController.apxc" using below code.
Note: Please make sure to enable 'Available for Lighting Experience' checkbox on Page Detail for "UpdateCustomMetadata.vfp". As we are going to use this page on Lighting Home Page using Lightning App Builder.
Step 5: Now lets go ahead an pull our VF Page on Lightning Home Page using Lightning App Builder.
Finally when I go ahead and update the values from VF Page, I should be able to view updated values as below.....
Below are some resource links for Metadata Api: