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

Suppose 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:

flowdata.get('start_reason/approval_reason/@ApprovalStatus')

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

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

You can add information to the flowdata so that it can be used by a downstream activity. Use the following expression in a post-activity map:

flowdata.start_reason/approval_reason/@ApprovalStatus

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:

process.getApprovalStatus()

You can enter this expression by opening the process node in the ECMAScript Objects 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:

flowdata.colors

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

flowdata.getObject('colors[1]')

Populating a List or Check Box Item

To populate list controls (for example, PickList or MVEditor) or the MVCheckbox control by 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, returns True:

CN=jdoe,ou=users,ou=idmsample,o=acme
cn=JDOE,ou=users,ou=idmsample,o=acme

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();