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
Namit PalNamit Pal 

How to use Custom Setting in apex?

Hi Everyone, I am very new to salesforce and am currently working on a sample application for Human Resource. 

I have stored the Employee Level and corresponding salary in a custom setting. So when i create a new employee, i should be able to select the employee level, which should auto populate the salary field using the custom setting field. Can anyone help me on how to do that?
Srinivasa Chary TaduriSrinivasa Chary Taduri
Example below, How to use in your scenario. Please change field names according to your application.

Map<String, CustomSettingName> vMapNameCustomSett = CustomSettingName.getAll();

for(Employee vEmployee: vLstEmployee)
{
    if(vMapNameCustomSett.containsKey(vEmployee.EmployeeLevel))
    {
        vEmployee.Salary__c = vMapNameCustomSett.containsKey(vEmployee.EmployeeLevel).Salary__c;
    }
}

Please mark it as best answer.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Namit pal,

May I suggest you please refer the below link for reference on using Custom Setting in an apex. hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Namit PalNamit Pal


Hi Srinivasa Chary Taduri, 

Thanks for a prompt reply. But don't i need to use a vf page for it? 

I am using this code in a trigger, but this is not working. 

The custom setting i created is a list type custom setting. Would that change anything? 

 

I am attaching a pic of the trigger i am creating. Please look at it once? 

User-added image

Srinivasa Chary TaduriSrinivasa Chary Taduri
Trigger event should be before insert. 

Please change to before insert and try
Srinivasa Chary TaduriSrinivasa Chary Taduri
for loop should be like below.  bold text
for(User Employee: trigger.new)
Varun SinghVarun Singh
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.


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.

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.User-added image


Let’s name this new setting as Org Defaults, and then click on save.

User-added image

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.
User-added image

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.

Help
Url:
https://webkul.com/blog/how-to-use-custom-settings-in-salesforce/

http://www.sfdc99.com/2014/03/02/change-your-code-on-the-fly-using-custom-settings/


https://webkul.com/blog/how-to-use-custom-settings-in-salesforce/

http://blogatsalesforce.blogspot.in/2015/03/use-custom-setting-efficiently-in-apex.html

https://focusonforce.com/configuration/the-wonders-of-custom-settings-in-salesforce/










 
Srinivasa Chary TaduriSrinivasa Chary Taduri
Replace "User" with "Employee" within the for loop
 like Employee.Salary
Employee.Level
Srinivasa Chary TaduriSrinivasa Chary Taduri
Updated below.

trigger SalaryDetails on User(before insert)
{
    Map<String, Salary_Information__c> CustomMap = Salary_Information__c.getAll();
    
    for(User Employee: trigger.new)
    {
        if(CustomMap.containsKey(Employee.Level__c))
        {
            Employee.Salary__c = CustomMap.get(Employee.Level__c).Salary__c;
        }
    }



​Please mark as best answer.
Namit PalNamit Pal

Dear Srinivasa,

The Code has just one error and i am not able to figure out the solution.

 

In the line Employee.Salary__c = CustomMap.get(Employee.Level__c).Salary__c;

The error says Salary__c does not exist.

I have changed the api names of fields from which i figured out that the console is not able to access the Salary__c field of custom setting. Rest all code has no error. How to go about this?


 

Varun SinghVarun Singh
Hi Namil 
First  you have to check  setting type  is  LIst Type 

Way to access list type custom setting
List<Salary_Information__c> mcs = Salary_Information__c.getall().values();
string ind=mcs[0].Level__c ;

your trigger
 
trigger SalaryDetails on User(before insert)
{
List<Salary_Information__c> mcs = Salary_Information__c.getall().values();
string sal=mcs[0].Level__c ;
    for(User Employee: trigger.new)
    {
        if(CustomMap.containsKey(Employee.Level__c))
        {
            Employee.Salary__c = sal;
        }
    }
}

I hope this is  useful for  you

Srinivasa Chary TaduriSrinivasa Chary Taduri
In the line Employee.Salary__c(Salary field should be from User object) = CustomMap.get(Employee.Level__c).Salary__c(Salary field should be from custom setting);
farukh sk hdfarukh sk hd
Let say we have custom setting called employee where we are storing employee roll number. 
Let's assume we have some employee information stored in custom setting. 
employee1     rollNo1 
employee2    rollNo2 

Syntax: 

Employee__c obj=new Employee__c.getvalues('employee1'); 
String rolNo=obj.rollNo1__c; 

For more details visit,

https://www.sfdc-lightning.com/2018/09/custom-settings-in-salesforce.html