Skip to main content
3 of 4
added 581 characters in body
Uwe Heim
  • 28.5k
  • 20
  • 121
  • 287

Is it possible to get a Custom Setting without the cost of a SOQL query and at the same time do it DYNAMICALLY without hardcoding the setting?

Once I had the illusion that accessing custom setting is always free and does not consume any governor limits. Until today: I've learned the hard way that this is only half-true.

It is true, that this comes without the cost of a query:

MyCustomSetting__c.getall().values();

But this DOES cost you a query

database.query('select Id from MyCustomSetting__c ');

Now how can I get the setting dynamically in a way that my code runs on every org regardless of the existence of MyCustomSetting__c? I need to hide the setting from Salesforce so that I can use something inside of a try/catch. If the setting does exist I need to get some values. If it does not exist I fall back on default values.

Additionally I need a way to update records of the settings synchronously in trigger contexts.

Can this be done free of charge of any query limit?


Update 2021-11-24

I've tried to use CustomMetadataType instead. It failed.

CustomMetadataType is similar to CustomSetting and looked very promising at the first glance. BUT in fact it is unusable for me, because it does not support DML to update records.

Records can be updated asynchronously with APEX. Not sure if this works in trigger context, but the possible timing issues are not acceptable for my requirements. I need updates of records in sync.

Uwe Heim
  • 28.5k
  • 20
  • 121
  • 287