Custom Setting and Custom Metadata in Salesforce.

With its two powerful tools, Salesforce allows you to store your application’s configuration data, personalize settings for users and profiles, and deploy dynamic configurations with ease. Read on to discover what Custom Setting and Custom Metadata are, their intriguing differences, and when to use one over the other.

featured image-custom-setting-and-metadata

In Salesforce, custom setting and custom metadata are two types of custom data storage that allow administrators and developers to store and manage custom configurations and data within the Salesforce platform. However, they both serve different purposes and have some key differences.

Let’s discuss each of them in detail.

Custom Setting

Purpose

The custom setting is designed to store configuration settings and application parameters, providing a centralized repository for static/infrequently changing information that can be accessed across various functionalities within the Salesforce environment.

Types of Custom Setting

1. List Custom Setting

List custom setting is ideal for scenarios where a uniform set of static data needs to be accessed across the entire organization. Suppose you frequently use data like “product categories” or “email notification defaults” over your application. In that case, you can house this data in a list custom setting to streamline access and eliminate the need for hardcoding or repeated data entry.

2. Hierarchy Custom Setting

Hierarchy custom setting allows you to define different values for the same setting to varying levels within the organization’s hierarchy. This can include settings at the organization level, profile level, or user level, providing a granular control mechanism tailored to specific user groups.

Access and Usage

  • You can access custom settings through the custom setting object in the Apex code or the Salesforce user interface.
  • It can be used in formulas, validation rules, Apex code, and workflow rules.

Deployment

Custom settings can be deployed between environments using Change sets or Packages.

  • Change sets bundle your custom setting metadata (the definitions) along with other customizations you want to deploy. They’re a simple and popular choice for quick transfers between closely related environments, e.g., between sandboxes or from sandbox to production.
  • Packages bundle components as modular building blocks that make up an application or piece of functionality that you are building. Packages are typically created so that their components can be clustered in a container for code migration, editing in an IDE, or distribution to other companies, as in the example of AppExchange packages.

Steps to Create a Custom Setting

  1. Log in to your Salesforce org.
  2. Click on the “App Launcher” (i.e., grid icon) and select “View All” or search for “Setup.”
  3. In Setup, enter “Custom Settings” in the quick find box and select “Custom Settings.” Click the “New Custom Setting” button.
  4. Choose the “List” / “Hierarchy” type to create that particular type of custom setting.
  5. Enter a unique name for your custom setting.
  6. Fill in other details, such as the label, description, and visibility.
  7. Add and define custom fields (optional) by clicking the “New Field” button.
  8. Click “Save” to create your custom setting.

Adding Data to Custom Settings

  1. Go back to Setup and navigate to “Custom Settings.”
  2. Click on the custom setting you created.
  3. Click the “Manage” button to add records to your custom setting.
  4. For the Hierarchy custom setting, you can define values at various levels (organization, profile, user).
  5. Click the “New” button to add records to your custom setting.
  6. Enter values for each field and click “Save” to add records.

Accessing Custom Setting Data in Apex

To access custom setting data in Apex, you can use the CustomSettingName__c.getInstance()method. For example:


MyCustomSetting__c customSetting = MyCustomSetting__c.getInstance();
String value = customSetting.MyField__c;

Custom Metadata

Purpose

Custom metadata is designed to store metadata, which includes configurations, settings, and data definitions. It is suitable for dynamic data that might change frequently or need to be versioned.

Types of Custom Metadata

Custom metadata supports both Custom Metadata Types and Custom Metadata Records.

  • Custom metadata types are similar to custom objects and are used to define the structure of metadata records.
  • Custom metadata records are instances of custom metadata types and store the actual data.

Access and Usage

  • Custom metadata is typically accessed programmatically through Apex code.
  • It is not directly accessible through the Salesforce user interface and cannot be used in formulas, validation, or workflow rules.

Deployment

Custom metadata can be deployed between environments using Change sets, Metadata API, or Salesforce DX.

Steps to Create Custom Metadata Types

  1. Log in to your Salesforce org.
  2. Click on the “App Launcher” (i.e., grid icon) and select “View All” or search for “Setup.”
  3. In Setup, enter “Custom Metadata Types” in the quick find box and select “Custom Metadata Types.”
  4. Click the “New Custom Metadata Type” button.
  5. Enter a unique name for your custom metadata type.
  6. Fill in other details, such as the label, description, and visibility.
  7. Add and define custom fields (optional) by clicking on the “Manage Records” button and then “Fields and Relationships.”
  8. Click the “Save” button to create your custom metadata type.

Steps to Create/Manage Custom Metadata Records

  1. Go back to Setup and navigate to “Custom Metadata Types.”
  2. Click on the custom metadata type you created.
  3. Click the “Manage Records” button to add records to your custom metadata type.
  4. Click the “New” button to add records to your custom metadata type.
  5. Fill in the values for each field.
  6. Click “Save” to add records to your custom metadata type.

Accessing Custom Metadata in Apex

To access custom metadata records in Apex, you can use SOQL (Salesforce Object Query Language) queries. Here is an example:


List customMetadataRecords =
[SELECT Id, Field1__c, Field2__c FROM
MyCustomMetadataType__mdt];




// Iterate through records
for (MyCustomMetadataType__mdt record : customMetadataRecords) {
String value1 = record.Field1__c;
String value2 = record.Field2__c;
}

Key Takeaways

Custom Setting vs Custom Metadata
  • Usage
    • Custom settings are generally used for storing static configuration data.
    • Custom metadata is more focused on storing metadata and dynamic data.
  • Access
    • Custom settings can be accessed through the user interface and used in various Salesforce functionalities.
    • Custom metadata is typically accessed programmatically in Apex.
  • Deployment
    • Custom settings can be easily deployed using Change Sets.
    • Custom metadata requires more advanced deployment methods like Metadata API or Salesforce DX.

Also read :

thumbnail image-salesforce-einstein
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *