Using complex variables and lists in JavaScript

You must initialize all complex variables and all lists (arrays) before you can use them in a process, service, or service flow. 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. In a service flow, references to business object types require that some variable of that type is declared in the service flow.
Important: If your heritage human service does not initialize variables, they are initialized 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 flow, 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, drag a script task from the palette onto the canvas.
  3. In the Implementation tab, initialize your variable by using 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.
    Important: The server script syntax in a heritage human service or service flow 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();
    Note: If the name of your complex business object conflicts with an existing JavaScript namespace, such as tw.object.property, tw.object.contentObject, or tw.object.toolkit, use the tw.object.baw namespace to instantiate the business object. For example:
    tw.local.myVariable = new tw.object.baw.property(); 
    tw.local.myListVariable = new tw.object.listOf.baw.toolkit();

What to do next

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