Let's consider an example where you want to set different approval limits for different user profiles using Hierarchy Custom Settings.
Create Hierarchy Custom Setting
- Navigate to Setup > Custom Settings.
- Click New Custom Setting and create a new setting named
Approval_Limits__c
with a field namedApproval_Limit__c
(a numeric field representing the approval limit).
Add Default Value
- In the Manage section of the custom setting, add a default value for the approval limit, let's say $10,000
Create Records for Specific Profiles
Create records for specific profiles with different approval limits. For example:
- Profile: Standard User, Approval Limit: $5,000
- Profile: Contract Manager, Approval Limit: $20,000
- Profile: Administrator, Approval Limit: Unlimited
Access Hierarchy Custom Setting in Apex
In your Apex code, you can access the approval limit based on the user's profile.
Decimal userApprovalLimit = Approval_Limits__c.getInstance().Approval_Limit__c;
This code will dynamically retrieve the correct approval limit based on the user's profile in the hierarchy.
Use Approval Limit in Logic
Use the retrieved approval limit in your business logic. For example:
Decimal purchaseAmount = calculatePurchaseAmount(); // Some business logic to calculate the purchase amount.
if (purchaseAmount <= userApprovalLimit) {
// Process the purchase approval logic.
} else {
// Display an error or take appropriate action for exceeding the approval limit.
}
This way, the approval limit is customized based on the user's profile, allowing for different approval thresholds for different profiles.
In this example, the hierarchy custom setting enables you to set default values and then override those values for specific profiles, providing a flexible way to manage approval limits across different user profiles in Salesforce.
Testing of Approval Limits
The FSRK_ApprovalLimitsTest class contains test methods to verify the correct setting of approval limits for different user profiles. Each method tests a specific use case, ensuring comprehensive coverage of the functionality.
@IsTest
public class FSRK_ApprovalLimitsTest {
public final static String STANDARD_USER = 'Standard User';
public final static String CONTRACT_MANAGER = 'Contract Manager';
public final static String SYSTEM_ADMINISTRATOR = 'System Administrator';
/**
* Verifies the correct setting of approval limits for a user with the "Contract Manager" profile.
*/
@IsTest
public static void test_approvalLimitForContractManager() {
User contractManager = generateTestUser(CONTRACT_MANAGER);
System.runAs (contractManager) {
insertCustomSetting(contractManager, 20000);
Decimal approvalLimitContractManager = FSRK_ApprovalLimits__c.getInstance().ApprovalLimit__c;
Assert.areEqual(20000, approvalLimitContractManager);
}
}
/**
* Verifies the correct setting of approval limits for a user with the "Standard User" profile.
*/
@IsTest
public static void test_approvalLimitForStandardUser() {
User standardUser = generateTestUser(STANDARD_USER);
System.runAs (standardUser) {
insertCustomSetting(standardUser, 5000);
Decimal approvalLimitStandardUser = FSRK_ApprovalLimits__c.getInstance().ApprovalLimit__c;
Assert.areEqual(5000, approvalLimitStandardUser);
}
}
/**
* Verifies the correct setting of approval limits for a user with the "System Administrator" profile
*/
@IsTest
public static void test_approvalLimitForSysAdmin() {
User sysAdmin = generateTestUser(SYSTEM_ADMINISTRATOR);
System.runAs (sysAdmin) {
insertCustomSetting(sysAdmin, null);
Decimal approvalLimitSysAdmin = FSRK_ApprovalLimits__c.getInstance().ApprovalLimit__c;
Assert.areEqual(null, approvalLimitSysAdmin);
}
}
/**
* Generates a test user based on the specified profile
* @param profileName The name of the Salesforce profile for the test user
* @return A User object representing the generated test user
*/
public static User generateTestUser(String profileName) {
String email = 'fsrk' + Datetime.now().getTime() + '@domain.invalid';
Profile profile = [SELECT Id FROM Profile WHERE Name = :profileName];
return new User(
Alias = 'vnavor', Email = email, EmailEncodingKey = 'UTF-8', FirstName = 'Viktor',
LastName = 'Navorski', LanguageLocaleKey = 'en_US', LocaleSidKey = 'en_US', ProfileId = profile.Id,
TimeZoneSidKey = 'America/Los_Angeles', Username = email
);
}
/**
* Inserts a custom setting with the specified approval limit and associates it with the user
* @param user a Salesforce User for whom the custom approval limit setting is being inserted
* @param approvalLimit indicates the specific approval limit value to be set in the custom setting
*/
public static void insertCustomSetting(User user, Decimal approvalLimit) {
FSRK_ApprovalLimits__c setting = new FSRK_ApprovalLimits__c();
setting.ApprovalLimit__c = approvalLimit;
setting.SetupOwnerId = user.Id;
insert setting;
}
}