Search code examples
javajbpm

Store Result of script Task into Global Variable/process variable


after invoking a GET REST api , we are processing the GET REST api Response in a script task like below and wanted to store the Filtered result into a Global Variable/process variable.

and then use the Global Variable/process variable to feed the next POST REST API.

Script Task code:

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++) {
  org.json.JSONObject  jsonObject = objects.getJSONObject(i);
    if (jsonObject.getString("card_id").equals(card_id)) {
        finalArray.put(jsonObject);
    }
} 
 FResult=kcontext.getVariable("Result");  #### in process we see FResult as null completely blank
System.out.println(FResult);

Solution

  • you have to use the setVariable() method. And don't forget to declare the FResult variable as global variable from the web designer.

    kcontext.setVariable("FResult", finalArray.toString());