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();
  • To evaluate ECMA expression in Designer using java script:

    ECMA expression: Date ()

    Java script to evaluate the Date () expression:

    function displayTime(){
     var time = new Date();
     time.getTime();
     }
    displayTime();

    You can use this java script for other expressions such as getDay(), getTime(), getFullyYear(), getFullYearUTC(), and getMonth().

    Similarly you can use java for the same expressions. For example: java.util.Date;

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 FirstName, which you have mapped in the Post Activity of the Start (Request) to flowdata.FirstName. To get the value of this field in another activity, you would use the following expression:

flowdata.get('FirstName')

You can use the Expression Builder to create this expression. In the ECMAScript Objects pane, click the arrow next to flowdata and select FirstName.

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

WARNING:Prior to Identity Manager 3.7, the name of the function used to compare DNs was “DNcompare.” With the 3.7 release, however, the name was changed to “dnCompare.” Please note that if you create one or more workflows that use “DNcompare” in a pre-3.7 Identity Manager environment and then migrate to version 3.7 or later, you must change all instances of “DNcompare” to “dnCompare” for your workflows to function properly.

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