4

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?

4

This can be accomplished with Apex Metadata API. Please check out this trailhead module:

https://trailhead.salesforce.com/en/content/learn/modules/apex_metadata_api

| improve this answer | |
2

There's a nifty gitHub library from Andrew Fawcett that makes doing this somewhat easier. Motivation and detail on his blog

| improve this answer | |
1

I have created one small utility which we can use to create, update custom metadata record. Here we just need to pass CMDT list and it will handle the insert/update.

//Update records
List<Test_metadata__mdt> MetadataList1 = [SELECT MasterLabel, DeveloperName,Name__c, Account_Number__c FROM Test_metadata__mdt]; //MasterLabel, DeveloperName is required when query as we need them in update
MetadataList1[0].Account_Number__c = 986532;
MetadataList1[0].Account_Number__c = 659823;
String jobId2 = MetaDataUtility.upsertMetadata(MetadataList1);

You can check that here.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.