+ Start a Discussion

Help with apex code



I am a new to apex so could somebody help me with this???


In my custom settings I have Object called MyPersonalSettings__c in my code I have something like:


settings = MyPersonalSettings__c.getValues('mypersonalsettings')


But it is possible to replace 'mypersonalsettings' with somethng like a method: for example getValues(method) where method could have something like that

if (condition == true)

  set mypersonalsettings

else if (condition  == false)

  set yoursettings

else ..


Could somebody provide my with some example, I will be very appreciate for that 





Hi Marcoppp,

I am not exactly sure I understand fully, but it sounds like the following might help. The code uses a scenario that will request a different settings depending on whether the user running the code is an admin or not.

User myUser = [SELECT Id, Profile.Name FROM User WHERE Id =: UserInfo.getUserId()];

Boolean isAdminUser = (myUser.Profile.Name == 'System Administrator') ? true:false;

String settingStringValue = getSettingName(isAdminUser);
MyPersonalSettings__c settings = MyPersonalSettings__c.getValues(settingStringValue);

private String getSettingName(Boolean isAdminUser) {
return isAdminUser?'adminSetting':'regularUserSetting'; // adminSetting and regularUserSettings are entries for your custom settings

Hope that helps



for custom setting you need to create instance and then call it values like this


yourCustomSetting__c custSet = yourCustomSetting__c.getOrgDefaults();


but it will return null if you havn't provided any value to you fields in Custom setting


take a look at this post




hit kudos(star) if this solution helped you




Thanks for the replies. But actually it is possible to replace this what I am calling inside I mean 'mypersonalsettings':


settings = MyPersonalSettings__c.getValues('mypersonalsettings')


with a method for example:


public static MyMethod(){

   //my logic



to have somethinf like:


settings = MyPersonalSettings__c.getValues(MyMethod)


Thanks again for help








No it is not.


You pass in the value for the setting you want...


All decision making for what value mypersonalsettings means must be done before you call getValue() or you can process the returned data after you get it.


You cannot pass a method in as the parameter.


Here is the documentation your need to refer to: