Using complex variables and lists in JavaScript

In Process Designer, you must initialize all complex variables and all lists (arrays) before you can use them in a process or service. After the variable is initialized, you can access and modify its properties. You can access predefined properties and functions to perform several operations.

About this task

Before you can set the properties of a business object and before you can add items to a list, you need to initialize your variable.

In a heritage human service, coaches and heritage coaches require that all variables are initialized.
Important: If your heritage human service does not initialize variables, IBM® BPM initializes them when the coach runs. This initialization occurs even when the coach does not use the variables. Because of this automatic initialization, ensure that your service level code does not require variables be in an undefined state.

Procedure

  1. In the Variables tab of your process or service diagram, declare a variable that is a complex business object or a list. For example, a variable named myVariable of type Requisition or a variable named myList that is a list of string variables.
  2. In the diagram area, drag a script task from the palette onto the canvas.
  3. In the Implementation tab, initialize your variable by using the a JavaScript text area:
    • If the variable is a complex object, use:
      tw.local.<variableName> = new tw.object.<businessObject>();
      For example:
      tw.local.myVariable=new tw.object.Requisition();
    • If the variable is a list, use:
      tw.local.<listName>=new tw.object.listOf.<businessObject>();
      For example:
      tw.local.myList=new tw.object.listOf.String();
    Remember: If your complex business object or list includes elements that are complex variables, they must also be initialized.
  4. In the Implementation tab, initialize your variable by using the a JavaScript text area:
    • If the variable is a complex object, use:
      tw.local.<variableName> = new tw.object.<businessObject>();
      For example:
      tw.local.myVariable=new tw.object.Requisition();
    • If the variable is a list, use:
      tw.local.<listName>=new tw.object.listOf.<businessObject>();
      For example:
      tw.local.myList=new tw.object.listOf.String();
    Important: The server script syntax in the heritage human service is different from the client-side script syntax in a client-side human service. For client-side human services, ensure that you use the standard JavaScript syntax to instantiate objects in the client-side script, instead of the server script syntax that is used in a heritage human service. For example:
    // To instantiate and populate a complex variable
    tw.local.customer= {};
    tw.local.customer.firstName = "Jane";
    tw.local.customer.lastName = "Doe";
    
    // To instantiate and populate an array
    tw.local.addresses = [];
    tw.local.addresses[0] = {};
    tw.local.addresses[0].city = "Boston";
    
    // To instantiate a String variable
    tw.local.customerID = "12345";
    
    // To create a Date variable
    tw.local.dueDate = new Date();

What to do next

You have initialized your complex variable or list. The variable can now store data.