Apply BeanShell in Bot Tasks
tip
You can use import directive from Bot Tasks as the most useful Java libraries are already included in SPA.
BeanShell does not support Generics!
Map<String, Integer> rpaVariables = new HashMap<String, Integer>(); // won't work in BeanShell
Map rpaVariables = new HashMap(); // use instead
To customize the Bot Task code, do as follows:
<script><![CDATA[
if(!reportLink.toString().trim().equals("")) {
Map secLink = new HashMap();
secLink.put("form8KURL", "https://www.sec.gov" + reportLink);
secLink.put("sicDetailsURL", "https://www.sec.gov" + link);
secLinks.add(secLink);
}
]]></script>
The script can return a value back to Web-Harvest:
<script return="reportsFixDoctype"><![CDATA[
String reportsFixDoctype = reportsPage.toString().replace("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">","");
reportsFixDoctype = reportsFixDoctype.replace("<!-- <html xmlns=\"http://www.w3.org/1999/xhtml\"> -->","");
reportsFixDoctype = reportsFixDoctype.replace(" ","");
]]></script>
All variables defined in WebHarvest OR injected from input data OR injected out of the previous step are available as a BeanShell variable wrapper. It is usually required to get a String value.
Access Web-Harvest variables from BeanShell and vice versa
<config>
<var-def name="webharvestVar">
<template>${var_from_input_data}</template>
</var-def>
<script></script>
<export include-original-data="true">
<single-column name="java_string_obj" value="${javaStringObj}"/>
</export>
</config>
Runtime WorkFusion variables available in Bot Tasks
There is a set of defined variables that you can use and access in the Web-Harvest XML configuration.
<config>
<var-def name="mytextval">
<template>${mytext}</template>
</var-def>
<var-def name="languagetype">
<template>${language_type}</template>
</var-def>
<var-def name="inputtext">
<script return='input'></script>
</var-def>
<var-def name="Translatevalue">
<script return='Translate'>
<![CDATA[
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import com.google.gson.JsonArray;
jsonstring=inputtext.toString();
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(jsonstring).getAsJsonObject();
rootObj.remove("from");
rootObj.addProperty("from",languagetype.toString());
rootObj.remove("text");
rootObj.addProperty("text",mytextval.toString());
String Translate = rootObj.toString();
]]>
</script>
</var-def>
<export include-original-data="true">
<single-column name="Translate_value" value="${Translatevalue}"/>
</export>
</config>
BeanShell supports special overloaded text forms of all common operators to make it easier to embed BeanShell scripts inside other kinds of documents, for example, XML. For more details, see the documentation.
Text form
|
Operator
|
|---|---|
| @gt | > |
| @lt | < |
| @lteq | <= |
| @gteq | >= |
| @or | || |
| @and | && |
| @bitwise_and | & |
| @bitwise_or | | |
| @left_shift | << |
| @right_shift | >> |
| @right_unsigned_shift | >>> |
| @and_assign | &= |
| @or_assign | |= |
| @left_shift_assign | <<= |
| @right_shift_assign | >>= |
| @right_unsigned_shift_assign | >>>= |
Usage example:
< while condition = "${!isMercuryTaskCompleted.toBoolean() @and !breakLoop.toBoolean()}" maxloops = "30" index = "ind" >