5

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?

2
  • Presume this is not API version related as covered in your earlier thread salesforce.stackexchange.com/questions/14771/…?
    – Keith C
    Commented Mar 27, 2014 at 11:20
  • API version 26.0. It's the same object, but as you can see I'm not even performing any operations here, just trying to see if it's null. Pretty sure there must be a platform bug at work.
    – Matt Lacey
    Commented Mar 27, 2014 at 21:35

1 Answer 1

2

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;
3
  • 1
    That behaviour changed around API 21.0.
    – Keith C
    Commented Mar 27, 2014 at 11:22
  • Interesting. Can you elaborate @KeithC? Commented Mar 27, 2014 at 11:28
  • In our older packages we do something similar to the code you posted, but in more recent ones rely on the object reference not being null. One of the answers here salesforce.stackexchange.com/questions/14771/… mentions the API version change too.
    – Keith C
    Commented Mar 27, 2014 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .