Am I taking crazy pills are is it unexpected behavior for the generic sObject getters to return null for boolean fields?


Was troubleshooting a null pointer error and noticed some strange behavior exhibited by this bizarre test

MyCustomSetting__c cs = MyCustomSetting.get('Key');
Boolean b1 = cs.boolean_field__c; // => 'false'
Boolean b2 = (Boolean) cs.get('boolean_field__c'); // => null
system.assertEquals(b1, b2); // fails
  • Is it for boolean type fields only? – Ashwani Mar 5 '14 at 17:47
  • It's only relevant for boolean fields since they're the only field null is really a false value. – Ralph Callaway Mar 5 '14 at 18:04
  • I have checked in my org i am getting correct values using your code. Assert passed. I can post the code I have used. – Ashwani Mar 5 '14 at 18:06
  • please, should help narrow down the circumstances where this happens – Ralph Callaway Mar 5 '14 at 18:19

I had the same issue years ago. The "Salesforce Primitive Data Types" are more complex than in Java. In Apex Boolean is an object and has to be initialized. You can assign "Apex Booleans" three different values: "true, false, null".

If you decelerate a Boolean variable the standard value is NULL. Therefore make sure that you initialize your variables to appropriate values before using them. For example, initialize a Boolean variable to false.

( Learn more about primitive data types: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_primitives.htm )


Sample code for Opportunity and Custom setting. Showing correct behaviour:

Opportunity cs = [SELECT IsPrivate FROM Opportunity LIMIT 1];
Boolean b1 = cs.IsPrivate; // => 'false'
Boolean b2 = (Boolean) cs.get('IsPrivate'); // => null
System.debug('@@ B1: '+b1+ ' - B2: '+b2);
system.assertEquals(b1, b2); // fails


23:23:53:079 VARIABLE_ASSIGNMENT [109]|cs|{"serId":1,"value":{"Id":"0069000000AygKwAAJ","IsPrivate":false}}|0x1c86f95

23:23:53:080 USER_DEBUG [112]|DEBUG|@@ B1: false - B2: false

//Map<id,AshCloud__ListType__c> csList = AshCloud__ListType__c.getAll();
AshCloud__ListType__c cs = AshCloud__ListType__c.getValues('ListName');
Boolean b1 = cs.AshCloud__BooleanField__c; // => 'false'
Boolean b2 = (Boolean) cs.get('BooleanField__c'); // => null
System.debug('@@ B1: '+b1+ ' - B2: '+b2);
system.assertEquals(b1, b2); // fails


23:31:54:079 VARIABLE_ASSIGNMENT [117]|cs|{"serId":1,"value":{"Name":"ListName","SetupOwnerId":"00D90000000jXtlEAE","LastModifiedById":"00590000001LjyRAAS","SystemModstamp":"2014-03-05T17:55:55.000Z","AshCloud_BooleanFie (5 more) ...":true,"CreatedById":"00590000001LjyRAAS","CreatedDate":"2013-09-25T09:36:56.000Z","IsDeleted":false,"LastModifiedDate":"2014-03-05T17:55:55.000Z","AshCloud_List1__c":"123","Id":"a0l900000008jotAAA"}}|0x6dcf5772

23:31:54:080 USER_DEBUG [120]|DEBUG|@@ B1: true - B2: true

  • Please ignore comments. They are copied from OP. However most of the code is from OP. – Ashwani Mar 5 '14 at 18:30
  • 2
    Not sure the true case is really helpful, since null never means true. Feels like a leaky abstraction, database probably has null it in the field, and Salesforce converts that to false nearly everywhere, except the dynamic getter. – Ralph Callaway Mar 5 '14 at 20:27

Generic sObject getters will give null value if you not re-queried your object. This is my finding :-

I have one object named test_Boolean__c and a check box field check_Boolean__c, whose default value is true.

test_Boolean__c obj = new test_Boolean__c();
insert obj;
Boolean x1 = obj.check_Boolean__c; // => true
Boolean x2 = (Boolean) obj.get('check_Boolean__c'); // => null

Using this will always give a null value for standard getter method.

Use like this for getting same behavior of fetching field by using both ways.

test_Boolean__c obj = new test_Boolean__c();
insert obj;
obj = [select check_Boolean__c from test_Boolean__c limit 1];
Boolean x1 = obj.check_Boolean__c; // => true
Boolean x2 = (Boolean) obj.get('check_Boolean__c'); // => true

Hope this will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.