Understanding Custom Metadata and Custom Settings in Salesforce

Share This Post

In the world of Salesforce customizations are key to tailoring the platform to your organization’s unique needs. Two important tools at your disposal for achieving this customization are Custom Metadata and Custom Settings. While both may seem similar at first glance, they serve distinct purposes and offer different benefits. In this blog post, we will delve into what Custom Metadata and Custom Settings are, how they differ, and when to use each one effectively.

Custom Metadata: A Deep Dive

What is Custom Metadata?

Custom Metadata in Salesforce is a powerful feature that allows you to create custom records to store application configuration data. This data can then be easily accessed and managed without the need for complex code changes. It’s essentially a flexible way to store and retrieve metadata about your Salesforce application.

Key Characteristics of Custom Metadata:

  1. Records, Not Objects: Custom Metadata records are similar to custom objects, but they are not meant to store transactional data. Instead, they store configuration settings, values, or metadata that can be used to customize your Salesforce application.
  2. Deployable: Custom Metadata records can be deployed between Salesforce orgs using Change Sets, Salesforce DX, or other deployment tools. This makes them a great choice for storing application configuration that needs to be consistent across different environments.
  3. No Code Required: You can create, update, and delete Custom Metadata records without writing code. This empowers administrators and developers to manage application settings without making changes to Apex code.
  4. Efficient Querying: Custom Metadata records can be queried using SOQL (Salesforce Object Query Language), making it easy to access and use the stored configuration data in your application.

When to Use Custom Metadata:

Custom Metadata is best suited for storing configuration data that doesn’t change frequently, such as picklist values, feature toggles, or settings that need to be consistent across multiple orgs or environments. It’s a good choice when you want to enable administrators to modify these settings without code changes.

Using Custom Metadata in Apex:

Accessing Custom Metadata:

To access Custom Metadata in Apex, you can utilize SOQL queries. Let’s consider an example where you have a Custom Metadata type named “Feature_Toggle__mdt” with fields “Name__c” and “Is_Active__c”:

List<Feature_Toggle__mdt> featureToggles = [SELECT Name__c, Is_Active__c FROM Feature_Toggle__mdt];

for (Feature_Toggle__mdt featureToggle : featureToggles) {
   String featureName = featureToggle.Name__c;
   Boolean isActive = featureToggle.Is_Active__c;
   // Use featureName and isActive as needed in your code
}

Modifying Custom Metadata:

It’s important to note that Custom Metadata records are primarily designed to be modified by administrators using Salesforce’s declarative tools. Consequently, making changes to Custom Metadata through Apex code is not recommended. Instead, you can deploy new versions of Custom Metadata records using deployment tools like Change Sets or Salesforce DX.

Custom Settings: A Closer Look

What are Custom Settings?

Custom Settings are another tool in Salesforce for storing custom data, but they differ in purpose and use cases compared to Custom Metadata. Custom Settings are designed to store data that is organization-specific and can be used across the entire organization or at a specific profile or user level. They are essentially custom objects with some unique characteristics.

Key Characteristics of Custom Settings:

  1. Hierarchy-Based: Custom Settings can be hierarchical, allowing you to define different values for different profiles or users. This makes them suitable for storing user-specific or profile-specific configuration data.
  2. Code-Based: Unlike Custom Metadata, Custom Settings require code (Apex) to create, update, and delete records. This means they are generally less accessible to non-developers and administrators.
  3. Data Access: Custom Settings can be accessed in Apex code, formulas, and validation rules, making them versatile for various use cases within the platform.

When to Use Custom Settings:

Custom Settings are ideal when you need to store configuration data that varies based on user profiles, specific users, or specific scenarios within your organization. They are also suitable for situations where you want to leverage hierarchy-based access to control settings for different groups of users.

Using Custom Settings in Apex:

Accessing Custom Settings:

Accessing Custom Settings in Apex involves using the getInstance() method provided by the Custom Setting’s API name. Let’s illustrate this with an example:

Suppose you have a Custom Setting called “Email_Settings__c” with a field “Email_Frequency__c” that you want to retrieve:

// Access the Email Settings custom setting for the current user's profile
Email_Settings__c emailSettings = Email_Settings__c.getInstance(UserInfo.getProfileId());

if (emailSettings != null) {
   String emailFrequency = emailSettings.Email_Frequency__c;
   // Use emailFrequency as needed in your code
}

This code retrieves the “Email_Settings__c” record associated with the current user’s profile and allows you to utilize the stored data in your Apex logic.

Modifying Custom Settings:

Custom Settings can be created or updated programmatically using Apex code:

// Create a new Email_Settings__c record
Email_Settings__c newEmailSettings = new Email_Settings__c(
   Name = 'ProfileIdOfNewSettings',
   Email_Frequency__c = 'Daily'
);
insert newEmailSettings;

// Update an existing Email_Settings__c record
Email_Settings__c existingEmailSettings = Email_Settings__c.getInstance('ProfileIdToUpdate');
if (existingEmailSettings != null) {
   existingEmailSettings.Email_Frequency__c = 'Weekly';
   update existingEmailSettings;
}

Conclusion:

Custom Metadata and Custom Settings are formidable tools for customizing Salesforce applications. Understanding how to access and manipulate them in Apex code is essential to unlock the full potential of Salesforce customization. Whether you’re configuring application settings, managing user preferences, or handling metadata, these tools empower you to craft tailored solutions that precisely align with your organization’s unique requirements. By choosing the right tool for each scenario, you streamline development, enhance application flexibility, and empower administrators to navigate configuration changes seamlessly.

I hope you like this blog and if you want any help let me know in the comment section.

Stay tuned, there is way more to come! Follow me on LinkedIn, Instagram, and Twitter. So you won’t miss out on all future articles.

Leave a Reply

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

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

NetSuite Salesforce Integration: An Automation Guide

NetSuite Salesforce Integration is the seamless connection between NetSuite, a leading cloud-based Enterprise Resource Planning (ERP) system, and Salesforce, a premier Customer Relationship Management (CRM) platform.