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
TehNrdTehNrd 

Bizarre behavior with checkboxes, null vs false

So I just burned a couple of hours trying to figure out what was breaking in my code. I figured it out but I still don't understand what is happening. You can paste the code below into the system log and execute.

 

 

Boolean someNullBoolean;
Contact c = new Contact(LastName = 'lname');
c.HasOptedOutOfEmail = someNullBoolean;
//c.HasOptedOutOfEmail should be null as it was contructed as null and we are also assigning the value of null, right? WRONG!

system.debug(c);
//cool, the value of HasOptedOutOfEmail is displaying as null, that makes sense
system.debug('HasOptedOutOfEmail; ' + c.HasOptedOutOfEmail);
//wait, what?!? HasOptedOutOfEmail is now false? I thought we set it to null

//eh, none of this should matter right? let's try to insert this contact
insert c;

//aw snap,
//System.DmlException: Insert failed. First exception on row 0; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, Email Opt Out: value not of required type: {0}: [HasOptedOutOfEmail]

Is this a bug, some expected behvior with booleans I don't understand, or am I in the matrix?

 

Thanks,

Jason

 

 

Message Edited by TehNrd on 08-18-2009 03:47 PM
ahab1372ahab1372

I get a compile error, I cannot set HasOptedOutOfEmail to someNullBoolean

 

 

11:19:08 DEBUG - Executing:

 

Boolean someNullBoolean;

Contact c = new Contact(LastName = 'lname'); c.HasOptedOutOfEmail = someNullBoolean;

//c.HasOptedOutOfEmail should be null as it was contructed as null and we are also assigning the value of null, right? WRONG!


11:19:08 INFO - Cumulative profiling information: No profiling information for SOQL operations. No profiling information for SOSL operations. No profiling information for DML operations. No profiling information for method invocations.


11:19:08 ERROR - Compile error: Invalid field HasOptedOutOfEmail for SObject Contact

 

 

 

Message Edited by ahab1372 on 08-20-2009 11:21 AM
TehNrdTehNrd

Hmm, it says this:

 

 

Invalid field HasOptedOutOfEmail for SObject Contact 

 

but I thought HasOptedOutofEmail was a standard field on the contact object.

ahab1372ahab1372

my mistake, Field-Level-Security was still set to nothing. Now I could reproduce it.

 

The DML error makes sense to me because a boolean field can be only TRUE or FALSE, not Null. It also makes sense that it comes up only when inserting/updating because as long as stuff is moved around in APEX, field validation rules are not executed.

 

However, it is somewhat incosistent that the Debug Log interprets the field value as FALSE if you log the field directly. Is it trying to be smart but fails halfway?

TehNrdTehNrd

Actually a boolean can be null, straight from the Apex docs:

 

Boolean: A value that can only be assigned true, false, or null.

ahab1372ahab1372
a variable in APEX yes, but a checkbox (boolean) field in the Database obviously cannot (which I think makes sense). The standard validation rules capture that upon insert/update and throw the error.
Message Edited by ahab1372 on 08-20-2009 12:17 PM
TehNrdTehNrd

I guess I assume if a value is null the database will intepret this as false upon insert/udpate.  It appears to try something like this in the Apex code but not on the database side.

 

I don't even think boolean is a possible data type for a table, at least with the type of database sfdc is using, so I think they store booleans as 1 and 0.

 

value not of required type: {0}

I think {0} referrs to a type of an integer with a length of 1.

 

 

 

 

Message Edited by TehNrd on 08-20-2009 12:27 PM
Message Edited by TehNrd on 08-20-2009 12:58 PM
ahab1372ahab1372

I agree it is not what one might expect, and it is definetly something one can find out only by try and error. So thanks for bringing this up, might save me some time one day

 

Arnt