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 BPM scripts, it is always best to initialize variables when they are declared so that TIBCO Business Studio’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 = com_example_scriptingguide_Factory.createCustomer();
c1.name = "Fred Blogs";
c1.custNumber = "C123456";
custList.add(c1);
c2 = com_example_scriptingguide_Factory.createCustomer();
c2.name = "John Smith";
c2.custNumber = "C123457";
custList.add(c2);

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

var c1 = com_example_scriptingguide_Factory.createCustomer();
c1.name = "Fred Blogs";
c1.custNumber = "C123456";
custList.add(c1);
var c2 = com_example_scriptingguide_Factory.createCustomer();
c2.name = "John Smith";
c2.custNumber = "C123457";
custList.add(c2);