Custom Setting in Salesforce - Admin Basic | Salesforce Funda


Custom settings are similar to custom objects in that they let you customize org data. Unlike custom objects, which have records based on them, custom settings let you utilize custom data sets across your org. Custom settings also let you distinguish particular users or profiles based on custom criteria.

Custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. This data can then be used by formula fields, validation rules, flows, Apex, and SOAP API

There are two types of custom settings :- 

List Custom Settings: These provide a set of records, each separated by a unique ‘Name’ value and provides a reusable set of static data that can be accessed across your organization.

 If you use a particular set of data frequently within your application, putting that data in a list custom setting streamlines access to it. Data in list settings does not vary by profile or user, but it is available organization-wide.

Hierarchy Custom Settings: These provide a record which is returned on the basis of the logged in user and the profile. This is used when we don’t want multiple records but instead if we want to use a hierarchy structure based on user/profile.

Uses a built-in hierarchical logic that lets you personalize settings for specific profiles or users. The hierarchy logic checks the organization, profile, and user settings for the current user and returns the most specific, or lowest, value. 

How to Create Custom Setting in Salesforce :-

1 ) Go to the Setup and Quick Find Box search for Custom Settings, Click on it 



2 )  Click on New Button, and Fill below details

        Label :- Country Phone Codes
Object Name :- CountryPhoneCodes
Setting Type :- List
Visibility :- Public
Description :- you can give accordingly
 
Note: If the setting type is disabled, go to schema settings from setup and enable “Manage list custom settings type”.



        click on save 


3 ) Click on New Button in Custom Fields Sections and fill below details 



      select field type as text

             


       Field Label :- Phone Code
       Lenght :- 10
       Field Name :- Phone_Code
   
       click on Next button


4 ) Click on Save Button


5 ) Click on Manage Button, Click on New Button, then fill details and click on Save and new

 


              Label :- India
      Phone Code :- +91



like this you can create from some more country and click on save

Create Hierarchy Type Custom Settings :-

1 ) :- Go to the Setup, Search for Custom Setting, Click on New button, 



2 ) :- Fill the below details 

        Label :- Discount
Object Name :- Discount
Setting Type :- Hierarchy
Visibility :- Public
Description :- Custom setting for Discount

click on Save



3 ) :- Create New Field, click on New Button in Custom Fields Section



4 ) :- Select the field type as percent, click on Next




5 ) :- Fill the below details 

         Field Label :- Percentage Allowed
 Length :- 18
Decimal Places :- 0
Field Name :- Percentage_Allowed
 
         Click on Next, Click on Save


6 ) :- Click on Manage button 



7 ) :- Click on New Button


8 ) :- Fill the below details 

       Location :- User/Profile ==> Select User and - Select for the User you want to select
       Percentage Allowed :- 30
 
       Click on Save



9 ) :- Click on Default Organization Level Value, New Button and create for default value



10 )  :- Fill the Default Values as :- 10




Accessing List Custom Setting Data :-

To access Custom setting in Salesforce using SOQL :-
Select FIELDS(ALL) From CountryPhoneCodes__c LIMIT 200

Select ID, Name, Phone_Code__c From CountryPhoneCodes__c LIMIT 200
List Type Custom Settings :- 
//getALL()
Map<string,CountryPhoneCodes__c> recsMap = CountryPhoneCodes__c.getAll();
for(String record : recsMap.keyset()){
System.debug( 'recsMap Name '+recsMap.get(record).Name+' recsMap Phone_Code__c '+recsMap.get(record).Phone_Code__c);
}
//get Specific values
CountryPhoneCodes__c rec = CountryPhoneCodes__c.getValues('India');
System.debug(rec);
Hierarchy Type Custom Settings :-
//Default Values
Discount__c rec = Discount__c.getOrgDefaults();
System.debug(rec);
//Based on UserId and Profile id
Discount__c rec = Discount__c.getInstance(UserInfo.getUserId());
System.debug(rec);
Feature :-