function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShreyankaShreyanka 

custom settings for roles

Hi everyone,

I have a requirement to create custom settings for roles (say for 6-7 roles) and call it in apex class where in the IF condition it should work (custom setting) for OR condition.

So, which custom setting should i create: Hierarchy or List custom settings? also, how to use it in apex class.

Can anyone help me on this.

Thanks in advance! 
Liam GattLiam Gatt

Heyo!

You should create a Hierarchy Custom Setting.

To use it in your Apex code just use the following syntax:

CustomSettingName__c settings = CustomSettingName__c.getInstance()

Just a warning that you can only assign them to individual Users or Profiles, or just have them as the Org Default. 

You could, alternatively, have custom metadata which you can retrieve and use depending on the role of the person.

To get user role, follow this question: https://developer.salesforce.com/forums/?id=906F00000008wAdIAI

SwethaSwetha (Salesforce Developers) 
HI Shreyanka,

The differences between Hierarchy Custom Settings & List Custom Settings:

> Hierarchy Custom Settings allow you to define different values for different profiles and roles within your organization's role hierarchy.
>You can access the value of a Hierarchy Custom Setting in Apex based on the user's profile or role.
>It is suitable when you need different values for each role or profile and want to leverage the hierarchical structure to provide default values for child roles.

> List Custom Settings allow you to define a single set of values that apply to all users within your organization.
>You can access the value of a List Custom Setting in Apex without considering the user's profile or role.
>It is suitable when you need a common value for all users or when the condition does not depend on specific profiles or roles.

Based on your requirement to create custom settings for roles (6-7 roles) and use them in an IF condition with an OR condition, it sounds like Hierarchy Custom Settings would be more appropriate.
 
// Assuming you have a Hierarchy Custom Setting named "MyCustomSetting__c" with fields "Role1__c," "Role2__c," etc.

MyCustomSetting__c customSetting = MyCustomSetting__c.getOrgDefaults();
if (customSetting != null) {
    if (UserInfo.getProfileId() == 'ProfileId1' && customSetting.Role1__c) {
        // Code for Role1 condition
    } else if (UserInfo.getProfileId() == 'ProfileId2' && customSetting.Role2__c) {
        // Code for Role2 condition
    } else if (UserInfo.getProfileId() == 'ProfileId3' && customSetting.Role3__c) {
        // Code for Role3 condition
    }
    // Add more conditions for other roles as needed.
}
Here, you should replace 'ProfileId1', 'ProfileId2', 'ProfileId3', etc. with the actual Profile IDs or Role IDs for your specific roles.

Related: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_custom_settings.htm

https://salesforcedrillers.com/learn-salesforce/custom-settings-in-salesforce/

https://focusonforce.com/configuration/what-is-custom-setting-in-salesforce/

If this information helps, please mark the answer as best. Thank you
ShreyankaShreyanka
Hi Swetha,

6 roles are associated with same profile & 1 role associated with the other profile. So in this case how we could create Hierarchy custom settings.

Could you please guide me.

Thanks in advance!