Dec 15, 2023
Salesforce, a powerful and flexible platform for customer relationship management (CRM), relies heavily on metadata to define and manage its data model. MetadataService API is a game-changer in the Salesforce ecosystem, providing developers with a robust toolset to programmatically create, update, and delete metadata components such as objects and fields. In this blog post, we'll explore how to leverage this API to perform essential tasks like creating objects and fields, upserting objects, and deleting objects and fields.
MetadataService API is a widely used Apex wrapper around Salesforce's Metadata API. It simplifies the interaction with Salesforce metadata by providing a set of Apex classes that mirror the structure of the Metadata API. This makes it easier for developers to work with metadata in a programmatic way, reducing the complexity of making direct Metadata API calls.
Creating custom objects in Salesforce is a fundamental step in tailoring the platform to meet specific business needs. With the MetadataService API, you can programmatically create objects using a few lines of Apex code. Here's a basic example:
public static void createObject()
{
MetadataService.MetadataPort service = createService();
MetadataService.CustomObject customObject = new MetadataService.CustomObject();
customObject.fullName = 'Test__c';
customObject.label = 'Test';
customObject.pluralLabel = 'Tests';
customObject.nameField = new MetadataService.CustomField();
customObject.nameField.type_x = 'Text';
customObject.nameField.label = 'Test Record';
customObject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { customObject });
}
Adding fields to custom objects is another common task in Salesforce development. The MetadataService API simplifies this process as well. Here's an example:
public static void createField()
{
MetadataService.MetadataPort service = createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'MyCustomObject__c.MyCustomField__c';
customField.label = 'My Custom Field';
customField.type_x = 'Text';
customField.length = 42;
List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { customField });
}
Upserting objects involves creating them if they don't exist or updating them if they do. The MetadataService API simplifies this process using the upsertMetadata method. Here's a snippet:
public static void upsertObject()
{
MetadataService.MetadataPort service = createService();
MetadataService.CustomObject customObject = new MetadataService.CustomObject();
customObject.fullName = 'Test__c';
customObject.label = 'Test';
customObject.pluralLabel = 'Tests Upsert';
customObject.nameField = new MetadataService.CustomField();
customObject.nameField.type_x = 'Text';
customObject.nameField.label = 'Test Record Upsert';
customObject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
List<MetadataService.UpsertResult> results = service.upsertMetadata(new MetadataService.Metadata[] { customObject });
}
public static void deleteObject()
{
MetadataService.MetadataPort service = createService();
String customObjectName = 'MyCustomObject__c';
List<MetadataService.DeleteResult> result = service.deleteMetadata('CustomObject', new String[] { customObjectName });
}
To delete a custom field using the MetadataService API, you can similarly use the deleteMetadata method. Here's an example:
public static void deleteField()
{
MetadataService.MetadataPort service = createService();
String fullName = 'MyCustomObject__c.MyCustomField__c';
List<MetadataService.DeleteResult> results = service.deleteMetadata('CustomField', new String[] {fullName});
}
MetadataService API empowers Salesforce developers to automate and streamline metadata management tasks. Whether you're creating custom objects, and fields, upserting metadata, or deleting components, this API provides a convenient and efficient way to interact with Salesforce metadata. By incorporating the MetadataService API into your development workflow, you can achieve greater agility and flexibility in customizing the Salesforce platform to meet your organization's unique requirements.
Reference: https://github.com/certinia/apex-mdapi
I hope this blog helped you!