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 atrue
orfalse
result. If the value results intrue,
then theIF-BLOCK
statements between the first "{}" (curly braces) are processed. If the value results infalse
the 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
cust
variable has the value null. Writingnull == cust
instead ofcust == null
can help if you forget to use " ==" and use" =" instead, sincecust = null
is valid in some places. However,null = cust
is never valid, so the syntax checker would help you in this case. - The "//" in the
else-block
is 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 will always refer to an object, so it will not be 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 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.