You need to sign in to do that
Don't have an account?

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?
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?
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.
May I suggest you please refer the below link for reference on using Custom Setting in an apex.
- http://blogatsalesforce.blogspot.sg/2015/03/use-custom-setting-efficiently-in-apex.html
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
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
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?

Please change to before insert and try
for(User Employee: trigger.new)
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.
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.
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/
like Employee.Salary
Employee.Level
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.
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?
First you have to check setting type is LIst Type
Way to access list type custom setting
your trigger
I hope this is useful for you
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