[SalesForce] Custom Metadata Manipulation in APEX

I am attempting to do some DML on a Custom Meta Data record, to allow users to have some "Defaults" when a managed package is installed, that we would also like them to be able to manipulate (and save via APEX).

However, I am receiving errors when I attempt to perform DML operations in Apex:

Default_Property_Mapping__mdt mapping = new Default_Property_Mapping__mdt();
mapping.Field_Name_c = 'test';
mapping.SFDC_Object_Name__c = 'Contact';
insert mapping;

The meta data type Visibility is set to Public and the Field Malleability is set to Subscriber Editable, however, I am thinking that the guide is ambiguous on this point that this does not make these "records" editable in APEX

Best Answer

Apex code can create, read, and update (but not delete) custom metadata records, as long as the metadata is subscriber-controlled and visible from within the code's namespace. DML operations aren’t allowed on custom metadata in the Partner or Enterprise APIs. With unpackaged metadata, both developer-controlled and subscriber-controlled access behave the same: like subscriber-controlled access.

Please refer for more details on custom meta-data

Related Topic