How to access metadata from APEX? – Summer’17 release feature
Access Metadata from Apex, What is metadata in salesforce? In Salesforce, if you create object, field, class, page, trigger and any other component type which represents org configuration and customization will be stored in the form of metadata. We can access this metadata by using Metadata API. With Summer’17 Salesforce release, we can use classes in the metadata namespace to access metadata components from Apex Code.
As per the salesforce release notes documentation, Access metadata from your Apex code to handle below tasks.
Customizing app installs or upgrades: During or after an install (or upgrade), your app can create or update metadata to let users configure your app.
Customizing apps after installation: After your app is installed, you can use metadata in Apex to let admins configure your app using the UI that your app provides rather than having admins manually use the standard Salesforce setup UI.
Securely accessing protected metadata: Update metadata that your app uses internally without exposing these types and components to your users.
Creating custom configuration tools: Use metadata in Apex to provide custom tools for admins to customize apps and packages.
This feature (Metadata access in apex) is currently limited to custom metadata records and page layouts. We can retrieve, create or update custom metadata but we cannot delete metadata through Apex.
- Use Metadata.Operations class to retrieve and deploy metadata.
- Use the Metadata.Operations.retrieve() method to synchronously retrieve metadata from the current org.
- Use the Metadata.Operations.enqueueDeployment() method to asynchronously deploy metadata to the current org.
Refer Salesforce Apex developer guide to know how to use this feature.