Modifying User Task by code

Hi everyone,

I will explain what I want. I want to get a User Task of a BpmnModel and set a list of CandidateGroups using
userTask.setCandidateGroups(candidateGroupList);

This works fine, because in the flowable tasks, the tasks appear to the candidate groups, but the problem occurs when server will be restarted, in other words, this information is not persisted. So … I am doubting if my code is okey or not.
The summary is that I want to manipulate the properties of the user tasks defined on the “Modeler” App of Flowable via Java

  ...
  ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  RepositoryService repositoryService = processEngine.getRepositoryService();

  // Get the process
  ProcessDefinitionQuery query = processEngine.getRepositoryService()
      .createProcessDefinitionQuery().latestVersion();
  List<ProcessDefinition> processDefinitions = query
      .processDefinitionKey("example").list();

  // Get the tasks
  if (processDefinitions.size() != 0) {
    BpmnModel bpmModel = repositoryService.getBpmnModel(processDefinitions.get(0).getId());
    List<UserTask> userTaskList = bpmModel.getMainProcess()
        .findFlowElementsOfType(UserTask.class);

    for (UserTask userTask : userTaskList) {
        List<String> candidateUserList = userTask.getCandidateGroups();
        if (observerApproval.getRole() != null) {
        List<String> candidateGroupList = userTask.getCandidateGroups();
        if (!candidateGroupList.contains(observerApproval.getRole().getId()))
          candidateGroupList.add(observerApproval.getRole().getId());
        userTask.setCandidateGroups(candidateGroupList);
      }
      ...

Thanks a lot!

The code you pasted above will only change the in-memory representation of the model.
If you want it persisted, you’d need to deploy it again through the repositoryService.createDeployment.addBpmnModel() and add your BpmnModel there.

Alternatively, if you don’t want a new deployed version, you can use the DynamicBpmnService, like this

ObjectNode processInfo = dynamicBpmnService.changeUserTaskCandidateGroups(taskId, candidateGroups);
dynamicBpmnService.saveProcessDefinitionInfo(instance.getProcessDefinitionId(), processInfo);

First of all, thanks for your answer.

I have coded your tips, but it doesn’t work again. I think that
your code is simulating the “Publish app definition”… but It doesn’t work.

image

When I execute my code and I print the bpmn model using:
byte[] xml = new BpmnXMLConverter().convertToXML(bpmModel);
System.out.println(new String(xml));

The candidate users appear, but when I restart tomcat, disappear. Also, when I assign a “candidateUserList”, in the UI Task, the process is not avaiable for all the assignated users. What can I do?

I have tried the above two lines to change the candidate group, which is not working for me.
Also its not giving any error or exception. Can anyone please help me out as I am a beginner.