Checking for Null Attributes

This section is intended chiefly for those readers not familiar with JavaScript.

The default value of the cust variable when the process starts is a special value written as null. If our script was running later on in the process, and there was a possibility that an earlier script might have set the cust variable to refer to a Customer, but it could still be null, then this can be checked in the script before calling the factory method, as shown below:

if (null == cust)
{
    data.cust = factory.com_example_data.createCustomer();
}
else
{
    // cust was assigned in an earlier script
}

There are several things to note here:

Once we know that the cust field has a value, we can then set the name. We can check to see if attributes have been previously assigned by comparing them against null, although this will only work for attributes that do not have a default value. For example:

if (data.cust == null)
{
    data.cust = factory.com_example_data.createCustomer();
}
/*
Set the cust.name if not already set
*/
if (data.cust.name == null)
{
    data.cust.name = data.customerName;
}

The example above shows how to use a multi-line comment. The comment is opened with a "/*", then all text until a matching "*/" is ignored by the script engine.

Similarly, you should check that an attribute is not null before using any methods on an object, as shown below:

if (data.cust.dateOfBirth != null)
{
    data.year = data.cust.dateOfBirth.getFullYear();
}

Otherwise you will get a runtime exception.