Loops Within Scripts

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

There are three different JavaScript loop syntaxes that can be used in BPM Script, and one that cannot be used. The ones that are supported are:

while (CONDITION) { BLOCK }
do { BLOCK } while (CONDITION);
for (INITIALIZER; CONDITION; INCREMENT) { BLOCK }

One that is not supported is:

for (FIELD in ARRAYFIELD) { BLOCK }

Here is a simple while loop that loops until the ix variable becomes less than 0:

var result = "";
var ix = 10;
while (ix >= 0)
{
   result = result + " " + ix;
   ix = ix - 1;
}

Things to note:

  • result = result + " + ix; can be written result += " " + ix; using abbreviations allowed in JavaScript.
  • ix = ix - 1; can be written ix--; or --ix; using abbreviations allowed in JavaScript.
  • ix++; or ++ix; can be used similarly instead of ix = ix + 1; .
  • The curly braces are required for loops in TIBCO Business Studio JavaScripts just as they are for if/else statements.

The do-while loop is very similar, but the condition is evaluated at the end of the loop, so the loop is always executed at least once:

var result = "";
var ix = 10;
do
{
   result += " " + ix;
   ix--;
}
while (ix >= 0);

The for loop is similar to the while loop, but has two extra expressions in it. One is executed before the loop starts and the other is executed at the end of each loop. It results in a more compact script. Here is the equivalent for loop to the above while loop:

var result = "";
for (var ix = 10; ix >= 0; ix--);
{
   result += " " + ix;
}

Looping Through Lists and Copying Elements in Lists

In order to iterate through a List, a ListIterator is used. First you need to obtain the ListIterator from the List using the listIterator() method. Two methods on the ListIterator are used to enumerate the items in the List: the hasNext() method returns true if there are more items in the list, and the next() method returns the next item in the list (or the first item in the list the first time it is called):

// Iterate through the list of customers to calculate the total
// credit limit:
var totalCreditLimit = 0;
for (var iterator=custlist.listIterator(); iterator.hasNext(); )
{
    // Get the first/next item in the list
    var customer = iterator.next();
    // add customer’s credit limit to total credit limit	
    totalCreditLimit += customer.creditLimit;
    // above statement is equivalent to:
    //totalCreditLimit = totalCreditLimit +     //          customer.creditLimit;
}

Note that when the three temporary variables: totalCreditLimit, iterator and customer are declared, they are all initialized so that TIBCO Business Studio knows what type they are. This helps with Script Validation and Content Assist.

If one of the inputs to a script is a field called oldOrder, which contains multiple Orderline Business Objects in an attribute called orderliness, and you want to copy these to another Order Business Object called order, then the orderlines List in the new Order object cannot just be assigned:

Instead, the instances in the List need to be copied over. You might think of doing it like this:

for (var iterator=oldOrder.orderlines.listIterator(); iterator.hasNext(); )
{
    var orderline = iterator.next();
    order.orderlines.add(orderline);
}

However, this should not be done, as Business Objects can only be contained in one Business Object. Attempting to add a Business Object to a second Business Object in a containment relationship could have unexpected consequences, and therefore should not be done (it will remove the object from the oldOrder Business Object). Instead, the content of the List needs to be copied over, as shown below:

for (var iterator=oldOrder.orderlines.listIterator(); iterator.hasNext(); )
{
  var orderline = iterator.next();
  var newOrderline = com_example_scriptingguide_order1_Factory.createOrderline();
  newOrderline.amount = orderline.amount;
  newOrderline.description = orderline.description;
  newOrderline.productCode = orderline.productCode;
  order.orderlines.add(newOrderline);
}

This can be simplified by using the ScriptUtil.copy() method to:

for (var iterator= oldOrder.orderlines.listIterator(); iterator.hasNext(); {
   var orderline = iterator.next();
   order.orderlines.add(ScriptUtil.copy(orderline));	
}

It can be simplified even more by using the ScriptUtil.copyAll() method, as shown below:

order.orderlines.addAll(ScriptUtil.copyAll(oldOrder.orderlines));

This method will copy all elements from the oldOrder.orderlines List to the order.orderlines List.