cancel
Showing results for 
Search instead for 
Did you mean: 

What is the process variable scope for?

goto_tony
Champ in-the-making
Champ in-the-making
I am using the activiti REST, before my process instance was initiated, I have to set values for the variables. And in next step, I use a user to login and check out his/her task, before "complete" command is fired, I have to set the previous variable value again,otherwise I can't retrieve the variables for following conditional flow determination.

I've tried to skip the variable value setting, but the REST prompt the error as "Did not retrieve the operation parameters".

Is that the variable scope always only for one task/event?
3 REPLIES 3

jbarrez
Star Contributor
Star Contributor
How do you set your process variables, ie which method do you use?
variables are normally process-scope, and you have to take care to make them task-scoped. So it should work as you describe.

goto_tony
Champ in-the-making
Champ in-the-making
hi,
I just use REST api to perform task operations.

http://activiti.org/userguide/index.html#N12B83
Performs an operation (claim or complete) on a task. For the "complete" operation additional variables (from a form) may be passed in the body. To read more about additional variables from forms, visit the Start Process Instance section

Request: PUT /task/{taskId}/[claim|complete]
{}
API: ProcessEngines.getProcessEngine(configuredProcessEngineName).getTaskService().xxx(taskId …)
Response:
{
  "success": true
}

If I didn't set variables value by JSON input, the following task will discard the original variable's value. So it seems strange for me according to your description.

jbarrez
Star Contributor
Star Contributor
I checked the code, and as far as I can see, all the input variables are stored as process variables, as the rest layer is just calling the complete() method on the taskService


@Put
  public ObjectNode executeTaskOperation(Representation entity) {
    if(authenticate() == false) return null;
   
    String taskId = (String) getRequest().getAttributes().get("taskId");
    String operation = (String) getRequest().getAttributes().get("operation");
    try {
      String startParams = entity.getText();
      JsonNode startJSON = new ObjectMapper().readTree(startParams);
      Iterator<String> itName = startJSON.getFieldNames();
      Map<String, Object> variables = new HashMap<String, Object>();
      while(itName.hasNext()) {
        String name = itName.next();
        JsonNode valueNode = startJSON.path(name);
        if (valueNode.isBoolean()) {
          variables.put(name, valueNode.getBooleanValue());
        } else if (valueNode.isLong()) {
          variables.put(name, valueNode.getLongValue());
        } else if (valueNode.isDouble()) {
          variables.put(name, valueNode.getDoubleValue());
        } else if (valueNode.isTextual()) {
          variables.put(name, valueNode.getTextValue());
        } else if("true".equals(valueNode.getTextValue()) || "false".equals(valueNode.getTextValue())) {
          variables.put(name, Boolean.valueOf(valueNode.getTextValue()));
        } else {
          variables.put(name, valueNode.getValueAsText());
        }
      }
     
      if ("claim".equals(operation)) {
        ActivitiUtil.getTaskService().claim(taskId, loggedInUser);
      } else if ("complete".equals(operation)) {
        variables.remove("taskId");
        [b]ActivitiUtil.getTaskService().complete(taskId, variables);[/b]
      } else {
        throw new ActivitiException("'" + operation + "' is not a valid operation");
      }
     
    } catch(Exception e) {
      throw new ActivitiException("Did not receive the operation parameters", e);
    }
   
    ObjectNode successNode = new ObjectMapper().createObjectNode();
    successNode.put("success", true);
    return successNode;
  }
}

If you follow that call, you'll see it ends up calling (in the CompleteTaskCmd class)


    if (variables!=null) {
      task.setExecutionVariables(variables);
    }
   
    completeTask(task);