
Understanding Metadata API in Salesforce | The Developer Guide
Metadata API
Salesforce Metadata API is utilized to help designers in retrieving, creating, deploying, updating, or deleting the customized information. It can be something that uses custom object definitions and page layouts for Salesforce organizations. The new Apex Metadata API makes it possible to build equipment to manipulate the metadata version, in preference to the records itself.
The most straightforward approach to get to the usefulness in Metadata API is to utilize the Salesforce Extensions for Visual Studio Code or the Ant Migration Apparatus. The two devices are based on the Metadata API and utilize the standard apparatuses to streamline working with Metadata API.
Don't forget to check out: An Introduction to Salesforce Force.com Migration Tool (ANT)
- The Salesforce Extensions for Visual Studio Code remembers apparatuses for producing for the Salesforce stage in the lightweight, extensible VS Code editorial manager. These instruments furnish highlights for working with improvement organizations (scratch organizations, sandboxes, and DE organizations), Apex, Aura parts, and Visualforce.
- The Ant Migration Tool is perfect on the off chance that you utilize content or the order line for moving metadata between a nearby registry and a Salesforce organization.
When you work in a multi-org environment. Saying you have 20 orgs, one for each of the states in which you do business. You use managed packages to update the orgs. You have created a custom field for Contact, and you want it to show up on the UI (page layout) in all your orgs. This problem can be solved with the help of metadata API.
How to Get Web Service WSDLs For Your Salesforce Organization
- Step1: Login into your org Account
- Step2: Go to Setup, enter API in the Quick Find box, then select API.
- Step3: Click Generate Metadata WSDL & Enterprise WSDL and save the XML WSDL file to your system.
- Step4: Next go to Quick Find box and enter Apex Class
- Step5: Convert WSDL to Apex ( there is a button )
How To Create Custom Object And Custom Fields Through Metadata API
Ex.
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 }); } public class Createfield_mdt { public static void createField(String objectName , String fieldName){ MetadataService.MetadataPort service = createService(); MetadataService.CustomField customField = new MetadataService.CustomField(); customField.fullName = objectName+'.'+fieldName+'__c'; customField.label = fieldName; customField.type_x = 'Text'; customField.length=20; service.createMetadata(new MetadataService.Metadata[] { customField }); } public static void CreateLookupField (String objectName , String fieldName){ MetadataService.MetadataPort service = createService(); MetadataService.CustomField customField = new MetadataService.CustomField(); customField.fullName = objectName+'.'+fieldName+'__c'; customField.label = fieldName; customField.type_x = 'Lookup'; customField.relationshipLabel = objectName; customField.relationshipName = objectName; customField.referenceTo = 'Opportunity'; service.createMetadata(new MetadataService.Metadata[] { customField }); } public static MetadataService.MetadataPort createService() { MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); return service; } }
Check out another amazing blog by Ayush here: What Is Custom Metadata type In Salesforce - All You Need to Know
Limitations of Metadata API
We can not perform delete operation, only read, create, and updates are supported. Metadata API using an asynchronous deploy, which is not compatible with Asynchronous tests.
Reference: trailhead.salesforce, marketplace.visualstudio