启动实例时设置的变量(或者taskService.setVariable设置),设置完后流程全局可见。
// 设置流程变量
Map<String, Object> variables = new HashMap<>();
// 办理人
variables.put("applyUserId", APPLY_USER_ID);
// 候选人
variables.put("candidateUsers", String.join(",", CollectionUtil.newArrayList("depLeader1", "depLeader2", "depLeader3")));
// 候选组
variables.put("candidateGroups", CollectionUtil.newArrayList("hrGroup1", "hrGroup2", "hrGroup3"));
String processDefinitionKey = "overtime";
String businessKey = processDefinitionKey + ":" + BUSINESS_ID; // 假设模拟业务id为1001
// 启动流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
System.out.println("processInstance = " + processInstance);
// 查询任务
Task task = taskService.createTaskQuery().processInstanceId(PROCESS_INSTANCE_ID).singleResult();
// 设置全局变量
taskService.setVariable(task.getId(), "applyUserId", "zhangsan");
任务办理时设置local流程变量,当前运行的流程实例只能在该任务结束前使用,任务结束该变量无法在当前流程实例使用,可以通过查询历史任务查询。设置完后只有该task可见。(或者taskService.setVariableLocal设置)
// 查询任务
Task task = taskService.createTaskQuery().processInstanceId(PROCESS_INSTANCE_ID).singleResult();
// 完成任务
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("departPass", true);
taskService.complete(task.getId(), hashMap, true);
// 查询任务
Task task = taskService.createTaskQuery().processInstanceId(PROCESS_INSTANCE_ID).singleResult();
// 设置局部变量
taskService.setVariableLocal(task.getId(), "departPas", true);
临时变量不做持久化存储,也就是不存储数据库。(taskService.setTransientVariable)
/**
* Similar to {@link #complete(String, Map)}, but allows to set transient variables too.
*/
void complete(String taskId, Map<String, Object> variables, Map<String, Object> transientVariables);