Calculation Writing Tutorial - 9.  Working with Booleans

Version 1

    Introduction

    This document is the ninth in a set of tutorials on writing calculations in Service Desk.  Each topic can be read independently but for an introduction to calculations please see the following document: Calculation Writing Tutorial - 1. What is a calculation? What is Boo?.

     

    Intended Audience

    Some experience in object and process design is recommended, as well as the basic concept of calculations as covered in the first tutorial (linked above).

     

    Comparing Boolean attributes

    When you refer to a Boolean in a calculation it is very simple to check its value:

     

    if Incident.IsClockStopped == true and Incident.IgnoreAgreements == false: ...
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

     

    You compare the value to true or false.  Note that these should not be in quotes and should be all in lowercase.

     

    Handling NULL values

    When you create a Boolean attribute its default properties will allow its value to be null as well as true or false.  The best practice is to avoid this which you normally do by setting the Is Mandatory property to True, the Backfill Value (when adding to an existing object) to False and the Default Value to False.  However if the attribute is not mandatory there is a chance that you need to consider the value being null when working with it in calculations.  You don't always need to explicitly check if the value is null as you do with some data types but you should be aware of how comparisons react:

     

    if Incident._MyNullBoolean == true: ... // this will treat null and false the same
    if Incident._MyNullBoolean != true: ... // this will also treat null and false the same
    if Incident._MyNullBoolean == false: ... // this will NOT treat null and false the same
    if Incident._MyNullBoolean != false: ... // this will NOT treat null and false the same
    
    
    
    
    
    

     

    You can fix the third example by explicitly checking for null first:

     

    if Incident._MyNullBoolean == null or Incident._MyNullBoolean == false: ...
    
    
    
    
    
    

     

    Or you could simply change the logic around to look like the second example.

     

    You might see some calculations that use the .ToString() suffix when making comparisons but this is not necessary and not recommended.

     

    Returning Boolean values

    To set the value of a Boolean attribute via a calculation you can simply return true or false without quotes:

     

    Value = false
    if Incident.Category.FullName == "Hardware":
        Value = true
    return Value