In the dynamic world of Salesforce, administrators, and developers are always seeking innovative solutions to streamline processes and enhance user experiences. Salesforce List Custom Settings, though deprecated, was once a powerful tool for storing customizable data at the organization, profile, or user level. This article will explore the creation process, practical use cases, and example implementations in Apex, and shed light on why Salesforce deprecated this feature. Additionally, we'll guide you on how to enable List Custom Settings in your Salesforce org.
Business Use Case
Imagine a scenario where a company has different types of products, each with its discount percentage. The company wants to apply discounts based on the type of product dynamically, and they want to have the flexibility to adjust these discounts without modifying the code. This is an ideal use case for List Custom Settings.
Creating a List Custom Setting
Create a List Custom Setting named "ProductDiscountSettings__c" with fields "ProductType__c" (Text) and "DiscountPercentage__c" (Number).
Why is List Custom Setting Type greyed out?
Note that after Spring '18, it will not be possible by default to select the "list" type for custom settings. To enable the creation of List type Custom Settings, administrators can activate "Manage List Custom Settings Type." In Lightning Experience (LEX), this can be done by navigating to Setup > Home > Data > Schema Settings, and in Classic, by going to Setup > Data Management > Schema Settings.
Populate the custom setting with records specifying the product types and their respective discount percentages.
Apex Code
Let’s create an Apex class that retrieves the discount percentage based on the product type from the List Custom Setting and applies it to a given product.
public with sharing class FSRK_ProductDiscountCalculator {
// Method to calculate discounted price based on product type
public static Decimal calculateDiscountedPrice(String productType, Decimal originalPrice) {
// Retrieve the discount percentage from List Custom Setting
Decimal discountPercentage = getDiscountPercentageByProductType(productType);
// Calculate discounted price
Decimal discountedPrice = originalPrice * (1 - discountPercentage / 100);
return discountedPrice;
}
// Helper method to get discount percentage from List Custom Setting
private static Decimal getDiscountPercentageByProductType(String productType) {
FSRK_ProductDiscountSettings__c discountSetting = FSRK_ProductDiscountSettings__c.getInstance(productType);
// If custom setting record exists, return the discount percentage; otherwise, default to 0%
return (discountSetting != null) ? discountSetting.DiscountPercentage__c : 0;
}
}
Now let's write a test class that will validate the accuracy of discount calculations:
@IsTest
public class FSRK_ProductDiscountCalculatorTest {
@IsTest
public static void testCalculateDiscountedPrice() {
// Create a test product and corresponding discount setting
FSRK_ProductDiscountSettings__c testSetting = new FSRK_ProductDiscountSettings__c(
Name = 'TestProduct',
DiscountPercentage__c = 15
);
insert testSetting;
// Test discounted price calculation
Decimal originalPrice = 100.00;
Decimal expectedDiscountedPrice = 85.00; // (100 * (1 - 15/100))
Decimal actualDiscountedPrice = FSRK_ProductDiscountCalculator.calculateDiscountedPrice('TestProduct', originalPrice);
// Assert that the calculated discounted price matches the expected value
System.assertEquals(expectedDiscountedPrice, actualDiscountedPrice, 'Discounted price calculation is incorrect.');
}
@IsTest
public static void testCalculateDiscountedPriceNoSetting() {
// Test scenario where no discount setting exists for the product
Decimal originalPrice = 100.00;
Decimal expectedDiscountedPrice = 100.00; // Default to 0% discount
Decimal actualDiscountedPrice = FSRK_ProductDiscountCalculator.calculateDiscountedPrice('NonExistentProduct', originalPrice);
// Assert that the calculated discounted price matches the expected value
System.assertEquals(expectedDiscountedPrice, actualDiscountedPrice, 'Discounted price calculation is incorrect.');
}
}
testCalculateDiscountedPrice: This method simulates a scenario where a discount setting exists for a product. The test sets up a test setting, calculates the discounted price, and asserts that the calculation matches the expected result.
testCalculateDiscountedPriceNoSetting: This method tests the scenario where no discount setting exists for a product. It calculates the discounted price in the absence of a setting and asserts the result against the expected value.
Key Tips with Custom Settings
SOQL Queries
When querying custom settings in Apex, prefer using the getInstance()
method for hierarchical settings and getAll()
for list settings. Keep in mind that SOQL queries on custom settings don't count towards SOQL query limits.
Access in Apex
Custom setting records can be accessed in Apex using the getInstance()
method for hierarchical settings or getAll()
for list settings. Ensure proper error handling, especially when dealing with hierarchical settings.
Visibility in Apex Tests
In Salesforce, custom setting records are not visible in Apex tests due to the concept of isolation of test data. Apex tests are designed to run in an isolated environment to ensure consistency and prevent unintended side effects on the actual data in the Salesforce org. Therefore, please don't forget to create custom settings directly in the test using insert new MySetting__c(MyFiled__c = 'value');
.
Hierarchy Priority
Understand the hierarchy of custom settings. Hierarchical settings prioritize organization-wide defaults, profile-specific, and user-specific values. List settings are not hierarchical and don't follow this priority.
DML Operations
For list custom settings, leverage DML operations like insert
, update
, and delete
to modify records programmatically.
Custom Metadata Alternative
Evaluate if Custom Metadata Types better suit your needs. They offer more features, are deployable, and have improved support for packaging compared to custom settings.