Create and Manage Custom Metadata Types Using CLI Commands

You can use the Salesforce command-line interface (CLI) to create custom metadata types, generate fields, create records, create records from a CSV file, and generate custom metadata types from an sObject.

Required Editions

Available in: Salesforce Classic and Lightning Experience

Available in: Enterprise, Performance, Unlimited, and Developer Editions

You can create, edit, and delete custom metadata type records from installed packages in: Group and Professional Editions

User Permissions Needed
To run custom metadata types CLI commands: Customize Application

Support for custom metadata types is available in the Salesforce CLI plugin version 49.0. See the Salesforce CLI Setup Guide for information about how to set up CLI, set up a developer hub, create a project, and create a scratch org.

Commands

The following commands are available to create and manage custom metadata types. For flags and usage information, use the --help flag. For example, sf cmdt generate records --help.

  • Create a custom metadata type.

    sf cmdt generate object

  • Generate a custom metadata field based on the specified field type. You can create fields within the metadata object folder or passed in the directory of the object folder.

    sf cmdt generate field

  • Generate a custom metadata type and all its records for an sObject. Use this command to migrate existing custom objects or custom settings to custom metadata types. The default directory is force-app/main/default/customMetadata.

    sf cmdt generate fromorg

    Note
    Note Custom Settings of type hierarchy are not supported.
  • Create a record for a specified custom metadata type.

    sf cmdt generate record

  • Insert new custom metadata type records from a CSV file.

    sf cmdt generate records

Considerations

  • Specify the object folder when creating custom metadata types or fields. For example, --output-directory force-app/main/dirObjects/Mycmdt.
  • Specify unique names when creating custom metadata types.
  • There are no restrictions on the number of records that can be inserted. When inserting a large number of records, be aware that the project deploy start command defaults to 33 minutes. The default is the number of minutes the command waits to complete and display results to the terminal window.
  • When using the cmdt generate records command, the DeveloperName identifier defaults to the column Name and is a required column. However, any column name can be specified by using the --name-column flag. Label is not supported as an alternative identifier.
  • The cmdt generate records command can be used to create new custom metadata types records or update existing custom metadata records.
Example
Example Create a custom metadata type that is protected along with the field types percent and checkbox. The metadata XML is created in the local directory for your SDFX project.
sf cmdt generate object --type-name Mycmdt --visibility Protected --output-directory force-app/main/dirObjects
sf cmdt generate field --name Checkbox --type Checkbox --output-directory force-app/main/dirObjects/Mycmdt
sf cmdt generate field --name Percent --type Percent --output-directory force-app/main/dirObjects/Mycmdt
Example
Example Generate a custom metadata type from a custom object. Use this command to migrate an existing custom object to a new custom metadata type.
sf cmdt generate fromorg --dev-name FromCustomObject --sobject MyCustomObject__c
Example
Example Insert records into an existing custom metadata type from a CSV file.

Create a CSV file and provide the API name of the custom metadata type in the insert command. For example,

Name CountryCode__c CountryName__c
Australia AU Australia
Brazil BZ Brazil
Canada CA Canada
sf cmdt generate records --csv ~/Downloads/CMT_CSV_country.csv --type-name CmdtCountry

Migrating Custom Settings and Custom Objects to Custom Metadata Types

When converting sObjects to a custom metadata type, unsupported object types are converted to a string format.

Mapping
sObject Type Conversion Type
Auto-Number Text field
Formula Converted based on formula return type. If it is of text type converting it into a long text area with default length of 32768
Lookup Text field
Roll-Up summary Text field
External lookup Text field
Master detail Text field
Encrypted text Unreadable text string.
Geolocation Two separate text fields representing the latitude and longitude
Multi-select picklist Text field
Time Text field
Currency Text field