You need to sign in to do that
Don't have an account?
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
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
Hmm, it says this:
but I thought HasOptedOutofEmail was a standard field on the contact object.
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?
Actually a boolean can be null, straight from the Apex docs:
Boolean: A value that can only be assigned true, false, or null.
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.
I think {0} referrs to a type of an integer with a length of 1.
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