I have added a script task in a workflow and added dependency in pom.xml like below. enter image description here
And Maven dependency is added .
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
<scope>provided</scope>
</dependency>
Builid is failing with error:
[KBase: defaultKieBase]: Process Compilation error Syntax error on token "import", throw expected org.json.JSONArray cannot be resolved to
But the build is failing. Could you please help
import org.json.JSONArray;
import org.json.JSONObject;
JSONArray objects = new JSONArray(Result);
JSONArray finalArray = new JSONArray();
for (int i = 0; i < objects.length(); i++) {
JSONObject jsonObject = objects.getJSONObject(i);
if (jsonObject.getString("ac_id").equals(acc_id)) {
finalArray.put(jsonObject);
}
}
in script task we never import classes, each time you call a class you have to type its complete path. this is applied not only for libraries, even if you create your own class (called Data Object in jBPM) you have to specifie its full package path. Even if you want to declare a String value you have to put java.lang.String name;
So here, you have to specify the path of JSONArray
and JSONObject
each time you use it. So you have to replace your code to
java.lang.String resTmp = (java.lang.String) kcontext.getVariable("Result");
org.json.JSONArray objects = new org.json.JSONArray(resTmp);
org.json.JSONArray finalArray = new org.json.JSONArray();
for (int i = 0; i < objects.length(); i++) {
JSONObject jsonObject = objects.getJSONObject(i);
if (jsonObject.getString("ac_id").equals(acc_id)) {
finalArray.put(jsonObject);
}
}
use kcontext.getVariable()
to get the actual value of the variable in the task, and each time you call a variable you have to cast it into its requested data type.