Retrieve and Deploy Metadata in Apex

Salesforce uses metadata types and components to represent org configuration and customization. Metadata is used for org settings that admins control, or configuration information applied by installed apps and packages.

Previously, accessing metadata required using the Metadata API, but you can now use classes in the Metadata namespace to access metadata components from Apex code.

Access metadata from your Apex code to handle tasks such as:

  • 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.

Metadata access in Apex is currently limited to custom metadata records and page layouts. You can retrieve, create, or update custom metadata, but you cannot delete metadata through Apex.

For more information and examples of how to deploy and retrieve metadata in Apex, see the Metadata feature documentation and Metadata namespace reference documentation in the Apex Developer Guide