In the documentation it says
Apex code can create, read, and update (but not delete) custom metadata records
No where in the guide do I see apex being used to update custom metadata, only to create and read. I'd like to update the custom meta data in my org with apex. Are you only able to edit custom meta data if it's in another org and part of a managed packaged?
Best Answer
The
Metadata.Operations
class gives the ability to update/retrieve/insert metadata.To answer your question about updating Custom Metadata, you'd be using the
Metadata.Operations.enqueueDeployement();
I've provided the apex class you can create in your own org to reproduce.
1st steps: Create a Custom Metadata Type. If you'd like to use the code I provide verbatim, then follow these steps
Open up your developer console, and execute
MetadataDeploy.updateCustomMetadata();
You can check the results in deployment status, or the logs where it says
QueueableHandler
.Check the Custom Metadata Record and see if it updated to 'FIeld Test 1 after', if it did, it successfully deployed the change.
TLDR: You can update Custom Metadata by using the
Metadata.Operations.enqueueDeployment(metadataContainer, callback)
. ThemetadataContainer
is basically setting up the package to deploy, which would beMetadata.CustomMetadata
, filling out the values that you want to change (Metadata.CustomMetadataValue
), and placing it in the metadata container(Metadata.DeployContainer.add(customMetadata)
. The callback is a way for the results to be handled, so if it failed or succeeded, you can get a response back.