11

One of the most basic and versatile sObject methods is get. For example:

My_Object__c record = [SELECT Name FROM My_Object__c LIMIT 1];
system.assertNotEquals(null, record.get('Name'));

However, this function no longer seems to work on Custom Settings unless I query for them explicitly. At least there is one checkbox field (among many) on my Custom Setting that is returning null.

final String EXISTING_NAME = 'Some Name that exists';
My_Custom_Setting__c recordByQuery = [
    SELECT SomeBoolean__c
    FROM My_Custom_Setting__c
    WHERE Name = :EXISTING_NAME];
My_Custom_Setting__c recordByGetAll = My_Custom_Setting__c.getAll().get(EXISTING_NAME);
My_Custom_Setting__c recordByGetInstance = My_Custom_Setting__c.getInstance(EXISTING_NAME);

system.assertEquals(true, recordByQuery.SomeBoolean__c); // succeeds
system.assertEquals(true, recordByGetAll.SomeBoolean__c); // succeeds
system.assertEquals(true, recordByGetInstance.SomeBoolean__c); // succeeds

system.assertEquals(true, recordByQuery.get('SomeBoolean__c')); // succeeds
system.assertEquals(true, recordByGetAll.get('SomeBoolean__c')); // fails (null)
system.assertEquals(true, recordByGetInstance.get('SomeBoolean__c')); // fails (null)

Should I report this as a bug? I am pretty sure this just stopped working, and that it should work. Has anyone else run into this issue?

  • 3
    Totally sounds like a bug. – ca_peterson Jun 20 '14 at 22:23
  • 1
    For what it is worth, I wasn't able to reproduce this using a list custom setting with a string field. Is it just this one boolean field? – Daniel Ballinger Jun 21 '14 at 8:53
  • Yes, we we have only run into this problem with Boolean fields. We have a large matrix of checkbox fields in list custom settings and some of the checkboxes read null unless they are queried for. I wish I could decipher a pattern to it all. – Adrian Larson Jun 24 '14 at 23:44
  • This sounds an awful lot like the whole checkbox initialization issue to me. We've notice that for any checkbox on custom objects that for whatever reason when initialized its null and doesn't reflect true/false until its been interacted with. Related perhaps? You could try writing some anonymous apex to loop over them and ding each one to get them initialized... – lifewithryan Jul 14 '14 at 22:00
2

Looks like conversion problem.
Try doing this:

system.assertEquals(true, Boolean.valueOf(recordByGetAll.get('SomeBoolean__c')));
  • LaceySnr is right. The get function returns Object versus an explicit type. You need to cast it to be able to compare it to a boolean or any other type. – Brent Sep 25 '14 at 3:03

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.