9.2 ECMAScript Examples

This section provides examples of common operations that you can perform using ECMAScript.

9.2.1 General Examples

To create a function in the ECMA expression builder, create the function inline:

function abc() { var v1 = "" ; for ( i = 0; i < 9 ; i++) v1 += "$"; return v1; } ; abc();

9.2.2 Flowdata Examples

This section presents scripting examples that show the use of the flowdata object.

Getting the Value of a Flowdata Variable

In the previous example, you entered information about an approval status into the flowdata by creating an XML element named start_reason with a child element named approval_reason and an attribute named ApprovalStatus. Use the following expression in a pre-activity map to retrieve the value of the ApprovalStatus attribute:


You can enter this expression by expanding the flowdata nodes in the ECMAScript Variables pane of the ECMA expression builder and double-clicking the ApprovalStatus attribute.

Figure 9-3 Selecting an Attribute

Creating an XML Element with Child Element and Adding it to the Flowdata

In the previous example, you retrieved user input to the form field ApprovalStatus. Now you can add this information to the flowdata so that it can be used by a downstream activity. Use the following expression in a post-activity map:


9.2.3 Form Control Examples

This section presents several examples of scripting with form controls.

Retrieving the Value of a Form Field

Suppose you have a form field named ApprovalStatus. To get the value of this field, use the following expression in a pre-activity map:


You can enter this expression by opening the Process node in the ECMAScript Variables pane of the ECMA expression builder and double-clicking ApprovalStatus.

Getting an Individual Value from a Multivalued Control

To get an individual value from a multivalued control (for example, a check box named colors), you first need to get the control into the flowdata. In the post-activity mapping for an upstream activity, use the following:


To get a value from colors (for example, the first value), use the following expression on a downstream activity:


Populating a List or Checkbox Item

To populate list controls (for example, PickList or MVEditor) or the MVCheckbox control using script, use an expression like this in the pre-activity mapping:

function list() {var l=new java.util.Vector();l.add(‘Blue’);l.add(‘Red’); l.add(‘Green’); return l;} list();

Comparing DNs

To compare DNs to find out if they are equal, use an expression like this:

if ( IDVault.DNcompare(flowdata.get('Activity3/CardRequest/Candidate'),recipient )) true; else false ;

This comparison is case-insensitive. For example, the following DNs, when compared with DNCompare, would return True:


9.2.4 Error Handling

The approach to handling errors differs between pre-activity and post-activity maps. For post-activity maps, you can use an error flow path from an Approval or Condition activity to catch errors that occur during post-activity mapping. This approach doesn’t work for pre-activity maps because any errors that occur in the process of getting data happen before the form is displayed to the user. When this occurs, an error message similar to the following appears in place of form controls in the bottom portion of forms displayed to the user:

XXXX FAILED to generate form due to: No data items are available!

In this scenario, you can use a try-catch statement in a source expression for a field in a pre-activity map:

function getTheData() {   var theData;   try {      theData = IDVault.get( 'cn=jsmith,ou=users,ou=idmsample1,o=acme' , 'user', 'FirstName') + ' ' + IDVault.get ( 'cn=jsmith,ou=users,ou=idmsample1,o=acme'  , 'user', 'LastName');     }   catch (error) { theData = 'Error retrieving data.'; }   return theData;};getTheData();