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
AbAb 

Use of Custom settings in Apex

Hello,

What iss the use of custom settings in Salsforce, mainly with triggers and apex class

thanks for suggestions !
Best Answer chosen by Ab
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for custom Setting
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_customsettings.htm
3) http://blog.jeffdouglas.com/2010/01/07/using-list-custom-settings-in-salesforce-com/
4) http://blogatsalesforce.blogspot.com/2015/03/use-custom-setting-efficiently-in-apex.html


Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user. All 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 the SOAP API.

There are two types of custom settings:

List Custom Settings
A type of custom setting that 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 with profile or user, but is available organization-wide. Examples of list data include two-letter state abbreviations, international dialing prefixes, and catalog numbers for products. Because the data is cached, access is low-cost and efficient: you don't have to use SOQL queries that count against your governor limits.

Hierarchy Custom Settings
A type of custom setting that 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. In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings.


Custom Setting Examples
The following example uses a list custom setting called Games. Games has a field called GameType. This example determines if the value of the first data set is equal to the string PC.
List<Games__C> mcs = Games__c.getall().values();
boolean textField = null;
if (mcs[0].GameType__c == 'PC') {
  textField = true;
}
system.assertEquals(textField, true);


Hierarchy Custom Setting Examples
In the following example, the hierarchy custom setting GamesSupport has a field called Corporate_number. The code returns the value for the profile specified with pid.
GamesSupport__c mhc = GamesSupport__c.getInstance(pid);
string mPhone = mhc.Corporate_number__c;


DON'T:
Don't query custom settings data using Standard Object Query Language (SOQL). It doesn't make use of the application cache and is similar to querying a custom object. 

Note: You can create up to 2 MB data in custom settings.



 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Sandrine,

Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data in an organization, profile, or specific user. All 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 the SOAP API.

There are two types of custom setting:
  1. List Custom Settings
  2. Hierarchical Custom Settings.
Use of Custom Settings:
Hard-coding and good code are a match made in hell. RecordTypeIds are most vulnerable to this abuse, and we’re are all familiar with the frustration of finding that code that worked in environment A, suddenly stops working when it is promoted to say test. Why? Because possibly RecordTypeIds are different across the two environments.

Why hard-code when you can use Custom Setting for stuff like this? Custom Settings are a SOQL inexpensive way of storing your configurable parameters in the safe confines of the Salesforce database, with all the conveniences of a custom object create/edit via point and click – standard salesforce!

Under the hood, Custom Settings are much like a Custom Salesforce Object, save for you cant have triggers on them. Most obvious difference, amongst others.

Custom Settings are also useful for admins, who aren't using them with code, too. They're often referenced in any feature which can use formulas - Validation / Workflow Rules and the Process Builder.

List Custom Settings are generally used with Apex code. But hierarchy CSs can be really useful. As the Salesforce guide (https://help.salesforce.com/apex/HTViewHelpDoc?id=cs_about.htm) mentions, they can be used to manage commissions/discounts (http://blog.jeffdouglas.com/2010/02/08/using-hierarchy-custom-settings-in-salesforce-com/) or to store lists which should contain different values, depending on which user accessing the list.

But by the far the most valuable use that I've come across, is incorporating hierarchy Custom Settings in your Validation Rules so that all rules can be switched off by toggling one checkbox - & not necessarily for all of your users at the same time but for a specific User or Profile.

As an administrator, this comes in handy when you need to mass update records using the DataLoader when you don't want Validation Rules to apply to you. If you don't like to exclude your profile from VRs by default - sometimes it's useful to know when the record that you're editing is incomplete.

This also comes in handy when a User needs to quickly make a particular change to a record which is being blocked because of an unanticipated circumstance.

This guide (http://simplysfdc.blogspot.co.uk/2014/01/mass-activate-or-deactivate-salesforce.html) explains how to incorporate hierarchy Custom Settings in Validation Rules.

Lastly, if you're using Visual Workflows, list Custom Settings are a great way to manage picklist values (http://cloud4good.com/announcements/flexibility-in-flows-custom-settings-and-dynamic-picklists/).
One of the best examples of using the custom setting in code is here:
http://www.sfdc99.com/2014/03/02/change-your-code-on-the-fly-using-custom-settings/

To Know more please check here:  Please mark this as best answer if the information helps.

Regards,
Nagendra.
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for custom Setting
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_customsettings.htm
3) http://blog.jeffdouglas.com/2010/01/07/using-list-custom-settings-in-salesforce-com/
4) http://blogatsalesforce.blogspot.com/2015/03/use-custom-setting-efficiently-in-apex.html


Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user. All 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 the SOAP API.

There are two types of custom settings:

List Custom Settings
A type of custom setting that 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 with profile or user, but is available organization-wide. Examples of list data include two-letter state abbreviations, international dialing prefixes, and catalog numbers for products. Because the data is cached, access is low-cost and efficient: you don't have to use SOQL queries that count against your governor limits.

Hierarchy Custom Settings
A type of custom setting that 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. In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings.


Custom Setting Examples
The following example uses a list custom setting called Games. Games has a field called GameType. This example determines if the value of the first data set is equal to the string PC.
List<Games__C> mcs = Games__c.getall().values();
boolean textField = null;
if (mcs[0].GameType__c == 'PC') {
  textField = true;
}
system.assertEquals(textField, true);


Hierarchy Custom Setting Examples
In the following example, the hierarchy custom setting GamesSupport has a field called Corporate_number. The code returns the value for the profile specified with pid.
GamesSupport__c mhc = GamesSupport__c.getInstance(pid);
string mPhone = mhc.Corporate_number__c;


DON'T:
Don't query custom settings data using Standard Object Query Language (SOQL). It doesn't make use of the application cache and is similar to querying a custom object. 

Note: You can create up to 2 MB data in custom settings.



 
This was selected as the best answer
Ankit Gupta SFDCLearnerAnkit Gupta SFDCLearner
http://blog.jeffdouglas.com/2010/01/07/using-list-custom-settings-in-salesforce-com/