So I have code along the lines of the following inside a managed package, and in one client org it's causing a null dereference exception.
double d = 100;
d *= (Global_Settings__c.GetOrgDefaults().Number_Field__c == null ? 1 : Global_Settings__c.GetOrgDefaults().Number_Field__c);
What's confusing me is that the documentation says GetOrgDefaults()
should return an empty object if no record exists (i.e. not null
) and in my experience that's always been the case. However, given the code above the only way I can be getting a null dereference is if GetOrgDefaults()
is returning null (the double var is known good).
So the question is whether GetOrgDefaults
is not supported on PE with hierarchical custom settings because of the lack of user profiles. Anybody have any idea?
Attribution to: Matt Lacey
Possible Suggestion/Solution #1
It's not just PE, getOrgDefaults()
has this strange behaviour in every edition unfortunately. The SObject itself returns null, not just the field. Here's typical code for making sure it always has a value:
//fetch custom setting or create it for the first time
Global_Settings__c setting = Global_Settings__c.getOrgDefaults();
if (setting == null) setting = new Global_Settings__c();
//write the values
setting.Number_Field__c = 1337;
upsert setting;
Attribution to: bigassforce
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31041