Useful Scripts

The following section illustrate a series of useful scripts.

Email Validation

To any input field, add a Condition Validator and add the following script to the condition attribute.

Copy
MATCH($emailAddress, '^[a-zA-Z0-9\\.\\-\\_]+@[a-zA-Z0-9\\.\\-]+$')

Code 1: Check valid email with Regular Expression

File Name Replacement

Copy
REPLACE($fileName, '[^a-zA-Z0-9]', '')

Code 2: Replace special characters

This script replaces all special characters from the filename. This script is useful when generating dynamic filenames from arbitrary user input to make sure the file names conform to any file system.

Swimlane Name

The following function returns the Swimlane Name:

Copy
WORKITEM().getSwimlaneName()

There are, however, a few restrictions:

  1. The function WORKITEM will return the actual active Workitem. This only works during the execution of a Process.
  2. The Function will always return the Swimlane Name of the actual Active Swimlane. If you are currently executing a Sub-Process, the Function will return the Swimlane Name of that current Swimlane. If you want to get the Swimlane Name from the root Workflow, you need to first obtain the root Workitem ID using the getRootWorkflowToken function, and then get the Swimlane Name of the root work item. For example, we have a sub-process with the Workitem ID 01219212122. To get the Swimlane Name of the root process, we would use the following:
    Copy
    WORKITEM(ProcessToken(01219212122).getRootWorkflowToken().getId()).getSwimlaneName()

Other Suggestions

Use comments in all your scripts, comment parameters and explain what the script does.

Copy
/**
Hello function says hello
@param $name the name to be hailed
@return a hail to the name :-)
*/
Function Hello(String $name) : String Begin 
   Return JOIN(Hello, $name);
End
// now, this comment is ok 

Code 4: Comments in scripts

CSV and Resources

How to read from a CSV File stored as a BusinessObject Resource and transfer the CSV content (records) to a DataStore database? Make sure you are using the right delimiter, default delimiter is set to ';' and make sure the headers of your CSV file correspond exactly to the attribute names of your DataClass. Also make sure no spaces are in the header names.

Copy
/**
Put ReadClientDataFromCSV description here ...
@param $message put parameter description here.
@return put return description here.
*/
Function ReadClientDataFromCSV() : Nothing Begin 
   // 1 extract from resource business object
   //
   String $text := READTEXTRESOURCE('ClientInformation');
   // 2 create temporary text file
   //
   String $tmpFile := JOIN('work/tmp/data-', UID(), '.csv');
   WRITETEXTFILE($tmpFile, $text);
   Indexed ClientInformation $cli := LOADFROMCSV('ClientInformation', $tmpFile, ',');
   // 3 write to database
   //
   Integer $i;
   For $i := 1 Condition $i <= $Collection:Size($cli) Step $i := $i + 1 Do 
      SaveDataEntity($cli[$i], 'MySQL');
   End
End

Secured Login Redirect

The following configuration lets you redirect to the Portal Process and enforces a login.

Copy
nm.window.start=REDIRECT:/internal/process

JavaScript and Expressions

Use Expressions to dynamically generate JavaScript commands:

Copy
Script
   Return JOIN('document.getElementById(\'IDOFHIDDENFIELD\').value=', $yourVariable.attribute);
End

Code 7: JavaScript and expressions

Login Test

The Functions Extension contains function JaasLoginTest, which can be used to test a username/password pair. The function relies on the JAAS API and therefore requires a JAAS application name. It returns true if a login action with the given username and password succeeded. Otherwise, false is returned.

Copy
Local Boolean $valid := JaasLoginTest('appway', 'nm', 'master')

Code 8: JaasLoginTest

Every FNZ Studio installation has a JAAS configuration file (usually called jaas.conf). The JAAS application name is the identifier before "{". In the example configuration below, the JAAS application name is appway.

Copy
appway {
com.nm.auth.jaas.NmLoginModule sufficient
debug="true"
url="http://localhost:8080/weblistener/anonymous/listener/LoginService"
secret="abcdef";
com.nm.auth.jaas.LdapLoginModule required
debug="true"
config="D:/Appway/5.1/data/conf/ldap.properties";
};
Note: A JAAS configuration file may contain multiple application sections. If you are unsure, have a look at Tomcat's server.xml configuration file. The JAAS application name should be defined on the JAASRealm.n

The function may be used to check credentials against LDAP servers. All you need to do is set up an LdapLoginModule in your JAAS configuration. If you use this function with the above configuration, users are checked again in FNZ Studio's internal user database or an LDAP server.

User Listing and Filtering

A very common task in process application is retrieving users from a specific group or role in order to implement reassignments dropdowns or Screens. To do this the following script can be useful.

Copy
Function GetNumcomUsers() : Indexed User Begin 
   Local Indexed User $users := ToIndexed(CONTEXT().getUserService().getUsersInGroup('Numcom'), User);
   Local User $user;
   Collection:Filter($users, EQUAL($user.getStatus(), 'ACTIVE'), $user, true);
   Return $users;
End

Code 16: List users

Detecting Cyclic Dependencies in Data Classes

Execute this script within the Interactive Script editor. To access Interactive Script, go to Studio > Administration > Script Language, and choose Start Interactive Script Editor from the small dropdown to the left of the tabs.

Copy
Indexed BusinessObject $bos := ToIndexed(CONTEXT().getDataClassManager().getDataClasses(null), BusinessObject);
BusinessObject $bo;
ForEach $bo In $bos Do 
   String $classId := $bo.getId();
   Indexed String $ext := ToIndexed(CONTEXT().getDataClassManager().findExtensions($classId, null), String);
   If $ext.contains($classId) Then 
      PRINTLN('Cyclic dependencies detected for class ', $classId, ' check depending classes: ', JOINLIST($ext, ','));
   End
End

Filtering Worklist by Attributes

Copy
Function TaskFilter(Indexed Workitem $worklist, String $name, String $value) : Indexed Workitem Begin 
   If $worklist == null Then 
      $worklist := WORKLIST();
   End
   Workitem $workitem;
   $worklist := Collection:Filter($worklist, TOSTRING($workitem.getAttribute($name)) == $value, $workitem);
   Return $worklist;
End
Copy
Indexed Workitem $worklist := WORKLIST();
$worklist := TaskFilter($worklist, 'Branch', 'NewYork');
$worklist := TaskFilter($worklist, 'Currency', 'EUR');