Working with Temporary Variables and Types

When writing scripts, the need for temporary variables often arises. In JavaScript, a temporary variable is declared using the var keyword. This saves having to add all variables as Process Data Fields, which is especially useful if the variables are only used within a single script. Adding them to the list of Data Fields would just end up complicating the process.

For example, to declare (that is, to tell the script engine about) two temporary variables called x and carName you can write:

var x;
var carName;

The variables can also be initialized (given initial values) when they are declared like this:

var x = 5;
var carName = "Herbie";

When writing TIBCO BPM Enterprise scripts, it is always best to initialize variables when they are declared so that TIBCO Business Studio - BPM Edition’s Script Validation and Content Assist code knows what type the variables are. If you do not initialize a variable, you will get the following warnings:

Unable to determine type, operation may not be supported and content assist will not be available

The content assist will not work. So, using the example from the previous section, it would not be a good idea to write:

var c1;
var c2;
c1 = factory.com_example_data.createCustomer();
data.c1.name = "Fred Blogs";
data.c1.custNumber = "C123456";
custList.push(c1);
c2 = factory.com_example_data.createCustomer();
data.c2.name = "John Smith";
data.c2.custNumber = "C123457";
custList.push(c2);

Instead, the variables should be initialized when they are declared:

var c1 = factory.com_example_data.createCustomer();
data.c1.name = "Fred Blogs";
data.c1.custNumber = "C123456";
custList.push(c1);
var c2 = factory.com_example_data.createCustomer();
data.c2.name = "John Smith";
data.c2.custNumber = "C123457";
custList.push(c2);