Apex Metadata API
Use cases for and notes on how to use the Apex Metadata API
Use Cases
- Update page layouts programmatically
- Nice for updates inside of managed packages -> can be paired with Apex post-install scripts
- Create new
CustomMetadatarecords- Using this you can create custom setup UIs for packages that users / admins fill out, which then programmatically configures packages via
CustomMetadata
- Using this you can create custom setup UIs for packages that users / admins fill out, which then programmatically configures packages via
Constraints
- Currently only supports two Metadata Types: Page Layouts &
CustomMetadatatype records - CRU functionality is supported (deleting not supported)
- No API for tracking status of metadata deployment; you have to implement a callback
- Deploying new Metadata is asynchronous, so you must utilize a Callback class (implements the
Metadata.DeployCallbackinterface) in order to respond to [un]successful deployments - You can deploy or retrieve Metadata in Post-Install scripts; however, you can only retrieve metadata in uninstall scripts
- Not built for Apex synchronous tests (due to the asynchronous nature of the deployments)
- See the Usage Notes for more info on testing
Usage Notes
- All classes / methods for interacting with the Apex Metadata API can be found under the
MetadataApex namespace - In order to deploy the Metadata updates, you must perform the following actions:
- Create a
Metadata Deployment Containerwhich will contain all of the Metadata changes to be deployed - Enqueue the deployment via
Metadata.Operations.enqueueDeployment(container, callback)
- Create a
- When writing tests for the code, focus on testing the following aspects (since you can’t test everything in the Apex Metadata API):
- Test that the
DeploymentContaineris set up properly - Test that the completion handler behaves as you would expect it to
- Test that the
Code Examples
Editing Page Layouts
Retrieve the Account Page Layout and add a new Field to it
public class UpdatePageLayout {
public Metadata.Layout buildLayout() {
List<Metadata.Metadata> layouts = Metadata.Operations.retrieve(
Metadata.MetadataType.Layout,
new List<String> {'Account-Account Layout'}
);
Metadata.Layout layoutMd = (Metadata.Layout) layouts.get(0);
Metadata.LayoutSection layoutSectionToEdit = null;
List<Metadata.LayoutSection> layoutSections = layoutMd.layoutSections;
for (Metadata.LayoutSection section : layoutSections) {
if (section.label == 'Account Information') {
layoutSectionToEdit = section;
break;
}
}
// Add the field under Account info section in the left column
List<Metadata.LayoutColumn> layoutColumns = layoutSectionToEdit.layoutColumns;
List<Metadata.LayoutItem> layoutItems = layoutColumns.get(0).layoutItems;
// Create a new layout item for the custom field
Metadata.LayoutItem item = new Metadata.LayoutItem();
item.behavior = Metadata.UiBehavior.Edit;
// NOTE: Field API name goes here
item.field = 'AMAPI__Apex_MD_API_sample_field__c';
layoutItems.add(item);
return layoutMd;
}
}
Callback Handler for Deploying Metadata
public class PostInstallCallback implements Metadata.DeployCallback {
public void handleResult(Metadata.DeployResult result,
Metadata.DeployCallbackContext context) {
if (result.status == Metadata.DeployStatus.Succeeded) {
// Deployment was successful, take appropriate action.
System.debug('Deployment Succeeded!');
} else {
// Deployment wasn’t successful, take appropriate action.
System.debug('Deployment Failed!');
}
}
}
Test Class for PostInstallCallback
@IsTest
public class MyDeploymentCallbackTest {
@IsTest
static void testMyCallback() {
// Instantiate the callback.
Metadata.DeployCallback callback = new PostInstallCallback();
// Create test result and context objects.
Metadata.DeployResult result = new Metadata.DeployResult();
result.numberComponentErrors = 1;
// NOTE: This is a null job ID. If you want to test a non-null Job ID, create a subclass
// of DeployCallbackContext and provide own implementation (see below)
Metadata.DeployCallbackContext context = new Metadata.DeployCallbackContext();
// Invoke the callback's handleResult method.
callback.handleResult(result, context);
}
}
// DeployCallbackContext subclass for testing that returns myJobId.
public class TestingDeployCallbackContext extends Metadata.DeployCallbackContext {
private Id myJobId = null; // Set to a fixed ID you can use for testing.
public override Id getCallbackJobId() {
return myJobId;
}
}
Metadata Deployment Container
public class DeployMetadata {
// Create metadata container
public Metadata.DeployContainer constructDeploymentRequest() {
Metadata.DeployContainer container = new Metadata.DeployContainer();
// Add components to container
Metadata.Layout layoutMetadata = new UpdatePageLayout().buildLayout();
container.addMetadata(layoutMetadata);
return container;
}
// Deploy metadata
public void deploy(Metadata.DeployContainer container) {
// Create callback.
PostInstallCallback callback = new PostInstallCallback();
// Deploy the container with the new components.
Id asyncResultId = Metadata.Operations.enqueueDeployment(container, callback);
}
}
Test Class for DeployMetadata
@IsTest
public class DeploymentTest {
@IsTest
static void testDeployment() {
DeployMetadata deployMd = new DeployMetadata();
Metadata.DeployContainer container = deployMd.constructDeploymentRequest();
List<Metadata.Metadata> contents = container.getMetadata();
System.assertEquals(1, contents.size());
Metadata.Layout md = (Metadata.Layout) contents.get(0);
// Perform various assertions the layout metadata.
System.assertEquals('Account-Account Layout', md.fullName);
}
}
Post-Install Script that Calls the Deployment
public class PostInstallScript implements InstallHandler {
// Deploy post-install metadata
public void onInstall(InstallContext context) {
DeployMetadata deployUtil = new DeployMetadata();
Metadata.DeployContainer container = deployUtil.constructDeploymentRequest();
deployUtil.deploy(container);
}
}
Creating New Custom Metadata Type Records
Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
customMetadata.fullName = 'MyNamespace__MetadataTypeName.MetadataRecordName';
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = 'customField__c';
customField.value = 'New value';
customMetadata.values.add(customField);
Metadata.DeployContainer container = new Metadata.DeployContainer();
container.addMetadata(customMetadata);
Id asyncResultId = Metadata.Operations.enqueueDeployment(container, null);
Updating Custom Metadata Type Records
public class VATController {
public final List<VAT_Rate__mdt> VATs {get;set;}
final Map<String, VAT_Rate__mdt> VATsByApiName {get; set;}
public VATController() {
VATs = new List<VAT_Rate__mdt>();
VATsByApiName = new Map<String, Vat_Rate__mdt>();
for (VAT_Rate__mdt v : [SELECT QualifiedApiName, MasterLabel, Default__c, Rate__c
FROM VAT_Rate__mdt]) {
VATs.add(v);
VATsByApiName.put(v.QualifiedApiName, v);
}
}
public PageReference save() {
// Create a metadata container.
Metadata.DeployContainer container = new Metadata.DeployContainer();
List<String> vatFullNames = new List<String>();
for (String recordName : VATsByApiName.keySet()) {
vatFullNames.add('VAT_Rate.' + recordName);
}
List<Metadata.Metadata> records = Metadata.Operations.retrieve(
Metadata.MetadataType.CustomMetadata,
vatFullNames
);
for (Metadata.Metadata record : records) {
Metadata.CustomMetadata vatRecord = (Metadata.CustomMetadata) record;
String vatRecordName = vatRecord.fullName.substringAfter('.');
VAT_Rate__mdt vatToCopy = VATsByApiName.get(vatRecordName);
// Update the values on the newly-fetched VAT Record
for (Metadata.CustomMetadataValue vatRecValue : vatRecord.values) {
vatRecValue.value = vatToCopy.get(vatRecValue.field);
}
// Add record to the container.
container.addMetadata(vatRecord);
}
// Deploy the container with the new components.
Id asyncResultId = Metadata.Operations.enqueueDeployment(container, null);
return null;
}
}
Security Implications
- Security for your org is enforced on 3 levels:
- Restrictions on the types of metadata that can be created / modified
- Will eventually support more than just Page Layouts & Custom Metadata Type records; but will never support the full Metadata API in Apex
- Salesforce doesn’t support automatic code generation (e.g., generating Apex classes or triggers); however you can get around it like DLRS using the actual API calls via Apex callouts
- Restrictions on the Apps that can deploy changes
- From certified managed packages that are provided by known, registered ISVs.
- Partner apps in AppExchange that can make metadata changes in a subscriber org are required to notify subscribers. Salesforce verifies this notice during the AppExchange security review.
- From uncertified managed packages, but only if the subscriber org enables a specific Apex setting
- Setup > Apex Settings > Deploy Metadata from Non-Certified Package Versions via Apex
- From unmanaged packages, which means that the code is owned by the org that executes it.
- From certified managed packages that are provided by known, registered ISVs.
- Detail audit histories logged for Metadata changes
- The namespace of the code performing the deployment is recorded
- Restrictions on the types of metadata that can be created / modified
- Deployments will fail if the user that enqueues the deployment doesn’t have the adequate permissions
- This generally means that Admins have to be the ones to trigger a queue-ing of an Apex Metadata deployment
- Note: Reading metadata values don’t require System Admin privileges, just requires that users have been added to the App