Hello,
I am trying to add candidate group to an existing task. It seems there is no direct method available in Java APIs to set candidateGroup to a task.
However I found that there is a REST API which does the same…
https://docs.camunda.org/manual/latest/reference/rest/task/identity-links/post-identity-link/
Does anyone have any corresponding code snippet to achieve the same through code.
Or any other way to set candidate group on a task programmatically.
Hi @Ashish_Pingale , Below code is used in the TaskListener implementation.
To set assignee:
delegateTask.setAssignee((String) delegateTask.getVariable(assignee));
To set candidate users:
private void updateCandidateUsersForTask(DelegateTask delegateTask, TaskEventRequest taskEventRequest) {
if (StringUtils.hasText(taskEventRequest.getCandidateUsers())
&& WorkflowUtil.hasProcessVariable(delegateTask.getExecution(), taskEventRequest.getCandidateUsers())) {
log.info("Update CandidateUser for task:{}, name:{}", delegateTask.getId(), delegateTask.getTaskDefinitionKey());
Set<IdentityLink> identityLinks = delegateTask.getCandidates();
if (!identityLinks.isEmpty()) {
identityLinks.stream().filter(identityLink -> IdentityLinkType.CANDIDATE.equals(identityLink.getType())
&& identityLink.getUserId() != null).forEach(identityLink -> {
log.info("Task " + delegateTask.getId() + " has an identity link for a user with id: "
+ identityLink.getUserId());
delegateTask.deleteUserIdentityLink(identityLink.getUserId(), IdentityLinkType.CANDIDATE);
});
}
String candidateUsers = (String) delegateTask.getExecution().getVariable(taskEventRequest.getCandidateUsers());
if (StringUtils.hasText(candidateUsers) && candidateUsers.lastIndexOf(',') > 0) {
Collection<String> candidateUsersList = Arrays.asList(candidateUsers.split(","));
delegateTask.addCandidateUsers(candidateUsersList);
} else {
delegateTask.addCandidateUser(candidateUsers);
}
}
}
To set candidate groups:
private void updateCandidateGroupsForTask(DelegateTask delegateTask, TaskEventRequest taskEventRequest) {
if (StringUtils.hasText(taskEventRequest.getCandidateGroups())
&& WorkflowUtil.hasProcessVariable(delegateTask.getExecution(), taskEventRequest.getCandidateGroups())) {
log.info("Update CandidateGroup for task:{}, name:{}", delegateTask.getId(), delegateTask.getTaskDefinitionKey());
Set<IdentityLink> identityLinks = delegateTask.getCandidates();
if (!identityLinks.isEmpty()) {
identityLinks.stream().filter(identityLink -> IdentityLinkType.CANDIDATE.equals(identityLink.getType())
&& identityLink.getGroupId() != null).forEach(identityLink -> {
log.info("Task " + delegateTask.getId() + " has an identity link for a group with id: "
+ identityLink.getGroupId());
delegateTask.deleteGroupIdentityLink(identityLink.getGroupId(), IdentityLinkType.CANDIDATE);
});
}
String candidateGroups = (String) delegateTask.getExecution().getVariable(taskEventRequest.getCandidateGroups());
if (StringUtils.hasText(candidateGroups) && candidateGroups.lastIndexOf(',') > 0) {
Collection<String> candidateGroupList = Arrays.asList(candidateGroups.split(","));
delegateTask.addCandidateGroups(candidateGroupList);
} else {
delegateTask.addCandidateGroup(candidateGroups);
}
}
}
aravindhrs:
delegateTask
Hi Aravindh,
This is useful.
Any idea how to get DelegateTask from Task. I Have the following code:
@Override
public void execute(DelegateExecution execution) throws Exception {
TaskQuery queryTasks = execution.getProcessEngineServices().getTaskService().createTaskQuery().processInstanceId(execution.getProcessInstanceId());
Task myTask = queryTasks.taskName(“Perform Processing”).singleResult();
// SET CANDIDATE GROUP HERE
}
Here’s the code:
public void execute(DelegateExecution execution) throws Exception {
TaskService taskService = execution.getProcessEngineServices().getTaskService();
Task task = taskService.createTaskQuery().processDefinitionKey(execution.getProcessDefinitionKey())
.processInstanceBusinessKey(execution.getProcessBusinessKey()).singleResult();
taskService.addCandidateGroup(task.getId(), "candidateGroup");
taskService.addCandidateUser(task.getId(), "candidateUser");
taskService.setAssignee(task.getId(), "assignee");
}
Is there a way to add multiple candidate users/groups as a list? TaskService just lets you add a single entry at a time. Basically, I want to avoid the listener being invoked for every candidate user insertion.