Prescripts and Postscripts

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 flow charts if you are not familiar with the supported scripting languages in general, or with the Flux API.

To set a script on an action or trigger, you can simply set the Prescript or Postscript.  For example, the following script creates the flow context variable "HELLO" with the value "Hello World".

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

Setting the Scripting Language

Flux uses the Bean Scripting Framework (BSF) environment in prescripts and postscripts. You can configure your Flux engines to use any of the scripting languages that are supported by this environment.

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 in a configuration file with the following syntax:

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

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.

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 place 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 Objects and Classes

When you use a pre- or post-script, there are several Objects and classes that are avaiable to you by default.

Objects

The predefined Objects are:

  • flowChart - the flow chart that is being executed.
  • action - the trigger or action that the script is executing from.
  • vm -  the flow chart'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: I am trying to add three string variables to the PreScript or PostScript of an action. Example of 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?

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>";

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.