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
Jesus Lorca AppsJesus Lorca Apps 

querying custom setting from a manage package is being cached

Hi, I am trying to query a custom setting but I am getting a very weird behaviour in a MANAGE package. Let's explain the case:

- Hierarchical Custom setting with a boolean field.
- VF page with a embedded LC. We do have a method in the controller to send emails. Through the boolean field we decide which behaviour we do want to have when sending emails.

- When I call the method to send emails from the LC, within the logic I query the boolean field from the custom setting, and then I have an if-else statement to decide the logic.

(the way I query the custom setting is:)

private static Boolean isQuarantineModeEnabled() {

MyCustomSetting__c config = MyCustomSetting__c.getInstance();

return config.MyBooleanField__c;

}

 

The "funny" thing that I don't understand is the fact that, after installing the manage package in a subscriber org, setting the right value to the custom setting boolean field, once I call the method to send emails from the LC, it does not work well  , although in the database the field has the right value, but for some reason salesforce still keeps the old value cached, and the if-else statement is not executed well.

 

Moreover, if from the developer console I just execute the anonymous apex, to refresh it:

MyCustomSetting__c.getInstance();   // NO updates in the database

 

then when I try to send emails again, it works, and it has the expected behaviour (the if-else statement works well). But unfortunately, after a while, it is stops working again, and you have to "refresh" it again. 

 

I am getting lost because of this as I have no idea why I am getting this behaviour. Does anybody have an idea about this? would be very useful! 

Dushyant SonwarDushyant Sonwar
Hierarchical custom setting will show different values for diferent profiles and users. when you call getInstance method then boolean value comes according to the current user or profile.

As per your case ,You can set the value in organisation default and use that value 
CustomSettingName__c mc = CustomSettingName__c.getOrgDefaults();

or

 you need to use list type of custom setting.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_customsettings.htm

Hope this helps.