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
dke01dke01 

Get a custom setting dynamically

Hi,

 

I want to return value of a custom setting as a string.

 

For example

 

MyCustomSettingInstance.getValueForField('foo');

 

Is this achievable?

 

Or can I do via dynamic SOQL somehow?

 

Like

query = 'SELECT '+ foo +' from MySetting__C where .......'

Then return just the String value for foo and not an sObject?

Best Answer chosen by Admin (Salesforce Developers) 
dke01dke01

I just noticed the sObject api has a GET method. This solved my issue calling MyInstance.get(string) returns the Object that I want.

 

so my code is now

 

 

String environment = Parameters__c.getValues('default').Environment__c;
Setting_AppDefaults__c setting = Setting_AppDefaults__c.getInstance(environment);
return setting.get(settingName);