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

Check if field (Custom Setting) exist

What is the best way to check if a custom setting exist?


I get my custom setting dynamically like so



MySetting__c setting = MySetting__c.getInstance('default');
return setting.get(settingName);



But if I pass setting name a field that does not exist - Then I get the following exception:

"System.SObjectException: Invalid field 'xxx' for MySetting__c"


And Silly APEX does not allow me to Try Catch this exception and handel it myself FFS.


So how can I check if a field exist on an Object.  Is the only way using a Describe call?


If any code tries to reference a field or object which doesn't exist in an org the system will not allow that to compile.

So the question of using try - catch to trap that doesn't arise.

Alternatively in case this is a requirment which is imperative for you to model your solution you can use as pointed by you DescribeObject to find the existence or absence of a field in an org.


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.