Prescripts and Postscripts (And Scripting in General)

To set a script on an action or trigger, you can simply set the Prescript or Postscript. The following sections explain how to use scripting in Flux using prescripts (run before the action/trigger where the script is configured) and postscripts (run after the action/trigger where the script is configured).

NOTE: Scripting in Flux requires some knowledge of the Flux API to operate successfully. It is not recommended to employ prescripts or postscripts in your workflows if you are not familiar with the supported scripting languages in general, or with the Flux API.

Setting the Scripting Language

Flux uses the Bean Scripting Framework (BSF) environment in prescripts and postscripts. You can configure your Flux engines to use Beanshell and/or Groovy. If you intend to use Groovy, you must download Groovy from https://groovy.apache.org/download.html and place the jar file into the Flux Installation lib directory - as well as make the engine-config.properties file change shown below.

If you do not specify a scripting language in the engine configuration, Flux will use BeanShell as the default scripting language.

You can set the scripting languages by adding the following lines to the engine-config.properties file, located in the Flux Installation Directory/config folder.

SCRIPTING_LANGUAGES.0.name=BeanShell
SCRIPTING_LANGUAGES.0.class=bsh.util.BeanShellBSFEngine

SCRIPTING_LANGUAGES.1.name=Groovy
SCRIPTING_LANGUAGES.1.class=org.codehaus.groovy.bsf.GroovyEngine'''

SCRIPTING_LANGUAGES.<number>.name specifies the name case-insensitive name of a scripting language. SCRIPTING_LANGUAGES.<number>.class specifies the class name for running the language in the BSF.

Creating a Flow Context Variable Using Scripts

To create a Flow Context variable and assign a value to it just call the following from any Prescript or Postscript:

flowContext.put("Variable_Name", "Variable_value");

This will create a variable named “Variable_Name” and assign the value “Variable_value” to it.

Retrieving Variables Using Scripts

Just like the line below creates and assigns a value to a Flow Context variable (or assigns a new value if the variable already exists):

flowContext.put("HELLO", "Hello World");

If you wish to retrieve the value of a Flow Context variable, you can call:

flowContext.get("Variable_Name");

Where “Variable_Name” is the name of the variable you are trying to retrieve.

If you need to retrieve a Runtime Variable or a Workflow Variable, you will need to call the following instead:

//Use this line of code to retrieve a runtime variable value
flowContext.substitute("${runtime VariableName}");

//Use this line of code to retrieve a workflow variable value
flowContext.substitute("${VariableName}");

The ‘${}’ syntax is used across Flux for Variable Substitution.

Overriding Action Results with Postscripts

A postscript can be used to override the result of an action or trigger. For example, a postscript can replace the standard result returned from a Java Action with a new value. Similarly, a postscript can replace trigger results with new results.

To override the result of an action or trigger, simply create a postscript on a particular action that contains the following line of script code:

flowContext.put("result", resultOverride);

The argument “resultOverride” is simply the value that you provide that replaces and overrides the result from the action or trigger itself.

The result of every action or trigger is placed into the flow context under the reserved name “result”. By placing your own value into that “result”, you can override and replace the result, if any, returned by the action or trigger.

For example, suppose a Java Action returns the string “my result value”. To override that result with the number 23, create a postscript on that Java Action that contains the following line of code:

flowContext.put("result", 23);

Similarly, to override the old string result “my result value” with a new string result “a new value”, create a postscript that contains this line of code:

flowContext.put("result", "a new value");

Alternatively, you can manipulate and replace the action results by first retrieving the result from the flow context, and then reinserting it. The code below demonstrates multiplying the current number in a For Each Number Action by four and returning it into the flow context:

String postscript = "double value = flowContext.get(/"i/");\n" +
"value *= 4;\n" + "flowContext.put(/"i/", value);";
forEachNumber.setPostScript(postscript);

Predefined Variables and Classes

When you use a pre or post-script, there are several variables and classes that are automatically available to you.

Variables

The predefined variables are:

  • flowChart - the workflow that is being executed.
  • action - the trigger or action that the script is executing from.
  • vm - the workflow’s variable manager.
  • flowContext - the current flow context.

Classes

When using BeanShell (the default scripting language), all of the flux.* classes (classes from the flux package) are available by default. If you need to use any other classes (for example, a flux.file.* class), you will need to import them at the top of the script, like so:

import flux.file.*;

Real-World Examples from our Technical Support Department

Question: How can I create a flow context variable containing the name of the current action (the action where the script executes)?

You can use the predefined object “action” to access the current action. You can call any method on this object that you could call on the action using the Flux API; so to get the action name, you would simply call action.getName().

You would then place the action in the flow context using the predefined object “flowContext”. flowContext.put(String, Object) adds an object to the flow context.

A prescript to add the action name to the flow context, therefore, would look something like:

flowContext.put("ActionName", action.getName()); You can then access the action's name from the flow context using standard API or variable substitution techniques.

Question: How do I show a message in the Operations Console?

A prescript to show a message to the Operations Console would look something like this:

flowContext.setStatus("Text of your choosing"); Such an approach is often used to provide status messages to operators during the processing of actions.

Question: I am trying to add three string variables to the PreScript or PostScript of an action. Example of a String variable:

String JobRequestXML1 = "<?xml version="1.0" encoding="UTF-8"?><job><input><file_input><uri>"; **This will upload but will not run in the engine. It is throwing a BSFException, I believe this is caused by the XML tags in the string. I have also tried to escape the < and > with &lt and %gt, but this has not worked either. How do I get XML into String Variables in a PreScript or PostScript of an action?**

A BSFException can occur when characters inside the string are not escaped properly. Instead of using the following:

String JobRequestXML1 = "<?xml version="1.0" encoding="UTF-8"?><job><input><file_input><uri>";

Use the following syntax to escape the double quote character (“):

String JobRequestXML1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><job><input><file_input><uri>";