#5256·Activiti

When creating multi-instance tasks, there always seems to be one task without an assigned person responsible.

Author: lovejk888Created Jan 3, 2026Updated Jan 3, 2026

Version used 7.0.0.Beta2 This is the BPMN file I defined.

  <process id="dynamicDepartmentProcess" isClosed="false" name="动态部门审批流程" processType="None">
    <startEvent id="startevent1"/>
    <userTask activiti:assignee="${currentAssignee}" activiti:exclusive="true" id="currentTask" name="当前审批">
      <extensionElements>
        <activiti:taskListener class="com.itheima.listener.DynamicDepartmentListener" event="complete"/>
      </extensionElements>
    </userTask>
    <userTask activiti:assignee="#{nextApproverList[loopCounter-1]}" activiti:exclusive="true" activiti:formKey="departmentForm" id="departmentTask" name="部门审批">
      <multiInstanceLoopCharacteristics activiti:collection="nextApproverList" isSequential="false">
        <loopCardinality><![CDATA[#{nextApproverCount}]]></loopCardinality>
      </multiInstanceLoopCharacteristics>
    </userTask>
    <endEvent id="endevent1"/>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="currentTask"/>
    <sequenceFlow id="flow2" sourceRef="currentTask" targetRef="departmentTask"/>
    <sequenceFlow id="flow3" sourceRef="departmentTask" targetRef="endevent1"/>
  </process>

The listener information is as follows:

@Slf4j
public class DynamicDepartmentListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        // 获取当前任务的处理人
        String currentAssignee = delegateTask.getAssignee();

        // 根据当前处理人决定下一级部门列表
        List<String> nextApproverList = getDepartmentsByCurrentUser(currentAssignee);

        // 获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        // 设置流程变量
//        runtimeService.setVariable(delegateTask.getProcessInstanceId(), "nextApproverList", nextApproverList);
//        runtimeService.setVariable(delegateTask.getProcessInstanceId(), "nextApproverCount", nextApproverList.size());

        delegateTask.getExecution().setVariable("nextApproverList", nextApproverList);
        delegateTask.getExecution().setVariable("nextApproverCount", nextApproverList.size()+1);
        //这里有bug,最后一个的负责人总是null
        //我指定是三个人,需要给定大小为4,才能完整展示,但是第四个还是null
        //设置大小为3会少一个负责人
        //不管了
        log.info("动态设置下一级部门: {}",nextApproverList);
    }

    private List<String> getDepartmentsByCurrentUser(String currentAssignee) {
        // 这里实现根据当前处理人决定下一级部门的逻辑
        // 例如:根据当前用户所属部门,查询需要审批的部门列表
        List<String> departments = new ArrayList<>();

        // 示例:根据当前用户返回部门列表
        if ("jack".equals(currentAssignee)) {
            departments.add("rose");
            departments.add("tom");
            departments.add("jerry");
        } else if ("director".equals(currentAssignee)) {
            departments.add("rose");
            departments.add("tom");
        }

        return departments;
    }
}

In this line of code("delegateTask.getExecution().setVariable("nextApproverCount", nextApproverList.size()+1);"), setting the size the same as the list causes the last assignee to be lost. Adding 1 to the size prevents the assignee from being lost, but then a task without an assignee appears (similar to the issue of losing the last assignee when the size is set the same as the list). Has anyone encountered this problem? Can it be solved by upgrading to the latest version of Activiti?