Newer Version Available
Operations Class
Namespace
Usage
Example: Retrieve Metadata
The following example retrieves the “MyTestCustomMDType” custom metadata record from the subscriber org, and inspects the custom fields.
1public class ReadMetadata {
2 public void retrieveMetadata () {
3 // List fullnames of components we want to retrieve
4 List<String> componentNameList =
5new List<String>{'ISVNamespace__TestCustomMDType.MyTestCustomMDType'};
6
7 // Retrieve components that are records of custom metadata types
8 // based on name
9 List<Metadata.Metadata> components = Metadata.Operations.retrieve(
10Metadata.MetadataType.CustomMetadata, componentNameList);
11 Metadata.CustomMetadata customMetadataRecord = (Metadata.CustomMetadata) components.get(0);
12
13 // Check fields of retrieved component
14 List<Metadata.CustomMetadataValue> values = customMetadataRecord.values;
15 for (integer i = 0; i < values.size(); i++) {
16 if (values.get(i).field == 'testField__c' &&
17 values.get(i).value == 'desired value') {
18 // ...process accordingly...
19 }
20 }
21 }
22}Example: Deploy Metadata
The following example uses the Metadata API in Apex to update the customField custom field value of the MetadataRecordName custom metadata record and deploy this change into the subscriber org. Because the deployment is asynchronous, you must provide a callback class that implements the Metadata.DeployCallback interface, which is then used when the queued deployment completes.
1public class CreateMetadata{
2 public void updateAndDeployMetadata() {
3 // Setup custom metadata to be created in the subscriber org.
4 Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
5 customMetadata.fullName = 'ISVNamespace__MetadataTypeName.MetadataRecordName';
6
7 Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
8 customField.field = 'customField__c';
9 customField.value = 'New value';
10
11 customMetadata.values.add(customField);
12
13 Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
14 mdContainer.addMetadata(customMetadata);
15
16 // Setup deploy callback, MyDeployCallback implements
17 // the Metadata.DeployCallback interface (code for
18 // this class not shown in this example)
19 MyDeployCallback callback = new MyDeployCallback();
20
21 // Enqueue custom metadata deployment
22 Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
23 }
24}Example: Create Two Metadata Records Synchronously
1public class CreateMetadata {
2
3 public Id doCreate(
4 String parentRecDevName,
5 String parentRecLabel,
6 String childRecDevName,
7 String childRecLabel) {
8
9 Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
10
11 Metadata.CustomMetadata parentRecord = new Metadata.CustomMetadata();
12 parentRecord.fullName = 'ParentType.' + parentRecDevName;
13 parentRecord.label = parentRecLabel;
14 mdContainer.addMetadata(parentRecord);
15
16 Metadata.CustomMetadata childRecord = new Metadata.CustomMetadata();
17 childRecord.fullName = 'ChildType.' + childRecDevName;
18 childRecord.label = childRecLabel;
19 Metadata.CustomMetadataValue relValue = new Metadata.CustomMetadataValue();
20 relValue.field = 'Parent__c';
21 relValue.value = parentRecDevName;
22 childRecord.values.add(relValue);
23 mdContainer.addMetadata(childRecord);
24
25 Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, null);
26 return jobId;
27 }
28
29}Operations Methods
The following are methods for Operations.
enqueueDeployment(container, callback)
Signature
public static Id enqueueDeployment(Metadata.DeployContainer container, Metadata.DeployCallback callback)
Parameters
- container
- Type: Metadata.DeployContainer
- Container that contains the set of metadata components to deploy.
- callback
- Type: Metadata.DeployCallback
- A class that implements the Metadata.DeployCallback interface. Used by Salesforce to return information about the deployment results.
retrieve(type, fullNames)
Signature
public static List<Metadata.Metadata> retrieve(Metadata.MetadataType type, List<String> fullNames)
Parameters
- type
- Type: Metadata.MetadataType
- The metadata component type.
- fullNames
- Type: List<String>
- A list of component names to retrieve. For information on component name formats, see Metadata.fullName().
Return Value
Type: List<Metadata.Metadata>