+ Start a Discussion

Access Custom Setting field with a string

Hi all,

Looking for some help on how to access a Custom Setting field with a String.

My use case is, I retrieve a month name as a string from a date eg. "January".
Thanks to that value I have to retrieve the value from the appropriate field in my Custom Setting instance (in this case January__c).

How can I convert my string to the metadata field name, knowing that these fields have the exact names + '__c'

Can anybody provide a snippet of code?


The code in the following post shows how to do want you're asking.  Let me know if you have any questions.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy.blogspot.com
Twitter: @GlynAtClosedWon
String theMonth = 'January';
My_Custom_Setting__c myCustomSetting = My_Custom_Setting__c.getInstance( 'MyCustomSettingName' );
String theFieldValue = (String) myCustomSetting.get( theMonth + '__c' );