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:
if (CONDITION) {IF-BLOCK} else {ELSE-BLOCK}is used for testing and conditionally executing statements. Between the "()" (parenthesis mark), there should be a condition that results in atrueorfalseresult. If the value results intrue,then theIF-BLOCKstatements between the first "{}" (curly braces) are processed. If the value results infalsethe statements between the second curly braces in the ELSE-BLOCK are processed. There can be multiple statements between the curly braces. These are referred to as a block of statements. In BPM Script, curly braces are mandatory. In JavaScript, they are only required if there is more than one statement to be processed.- The "==" operator is used to test for equality. Here, it is being used to test if the
custvariable has the value null. Writingnull == custinstead ofcust == nullcan help if you forget to use " ==" and use" =" instead, sincecust = nullis valid in some places. However,null = custis never valid, so the syntax checker would help you in this case. - The "//" in the
else-blockis used to introduce a comment. The rest of the line following "//" is ignored by the script processing engine. - If a UserTask is processed that has a BOM field as an Out or In / Out parameter in its Interface (the default for all fields is In / Out), then after the UserTask is complete the BOM field always refers to an object, so it is not necessary to initialize the BOM field from the Factory method in any scripts that follow the UserTask that outputs the field.
- This is also true for any other task that has a mandatory Out or In / Out parameter (the difference between UserTasks and Forms is that it always creates objects, even for Optional parameters).
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 only works 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 might get a runtime exception.