Quite some time ago, I wrote a short article on using the Bit attribute type within CRM.

Today, I thought I'd like to take some time to review some tips that may not be totally obvious when you first start working with Bit attributes.

The method that you will use to interact with the Bit attribute will depend on how the attribute is displayed.  And, if you should change the format of the Bit Attribute, if could affect the way your code functions.

Checkbox or Radio Button Formats

If the Bit attribute is displayed as either a Checkbox or a Radio Button, you reference the Attribute's value as a boolean field

Check for True or False:
if (crmForm.all.new_mybitfield.DataValue)
{
    // DataValue is true, do something interesting
}
else
{
    // DataValue is false, do something interesting
}
Setting the Value:
crmForm.all.new_mybitfield.DataValue = true;
// or
crmForm.all.new_mybitfield.DataValue = false;

List Format

The List format is actually converted into a Picklist with values of No and Yes.  Since it is a Picklist, we can't use the above code.  With this being the case, access and manipulation of the data is totally different.  Instead of a boolean value, the List format of a Bit Attribute uses an integer instead:

NO = 0

YES = 1

Check for True or False:
if (crmForm.all.new_mybitfield.DataValue == 0)
{
    // The answer is NO
}
else
{
    // The answer is YES.
}
Setting the Value:
crmForm.all.new_mybitfield.DataValue = 0; // No
// or
crmForm.all.new_mybitfield.DataValue = 1; // Yes

 

Attempting to assign a true or false value to a List-formatted Bit Attribute will result in an error dialog box being displayed.

Well, that about covers it. If anyone has any further suggestions, please add a comment to the post.