How to use custom settings in salesforce
We have all made a custom app in salesforce, be it a developer or administrator. However developers have also created apps to upload packages on appexchange, Salesforce’s own app store. With custom apps comes custom settings for that object, variables which will be used quite often. The best way to store them is by making another object, and using it as it is to store those settings as records. But then there is the problem of accessibility, various users might not have proper access on those objects, and therefore the app won’t work as expected. Well salesforce has provided us with a solution, and that’s what I’ll demonstrate you, how to use custom settings in Salesforce.
So what are these custom settings? Well the answer is simple, these custom settings are like custom objects, but have an organization wide access. Data can be added in them like any Object, through APEX code, or by creating new record. However the access is available organization wide.
Types of Custom Settings
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.
- 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.
Example
For this example we are going to focus on hierarchy type settings. Once created these settings can be used even in formula fields or normally in APEX code to store information which can be only changed by administrators.
First we have to go to Setup| Develop| Custom Settings and create a new setting.
Let’s name this new setting as Org Defaults, and then click on save.
As you can see that the settings is like an object, having a new button for custom fields. You can create as many custom fields as you want and store data in them.
After creating custom fields you can access this from APEX code too. Simply fire a SOQL Query in APEX and use the name of the setting you created in the place of object name.
OrgDefaults__c obj = [select id, Org_Code__c from OrgDefaults__c];
system.debug(obj);
Also You can add this field in other custom formula fields, directly by selecting the field name in insert field. You can also create records in this object with the help of APEX code.
Support
That’s all for how to use custom settings in salesforce, for any further queries feel free to contact us at:
https://wedgecommerce.com/contact-us/
Or let us know your views on how to make this code better, in comments section below.