cancel
Showing results for 
Search instead for 
Did you mean: 

Assign an user task during the creation event

kse
Champ in-the-making
Champ in-the-making
Hi all,

Activiti version: 5.8

Is there a way to assign an user task when the creation event is intercepted ?

What I want to do is when a task is created, I would like to assign it to an user.
I'm using the interface TaskListener to intercept the create event. In the notify method, I'm trying to set assignee but the task does not exist yet in database. Then there is a ProcessEngineException which has sens.

Here is the code:


public final class TacheSuspendueTaskListener implements TaskListener {

    @Autowired
    private UtilisateurRepositoryService utilisateurRepositoryService;

    @Autowired
    private MoteurProcessusService moteurProcessusService;
   
    @Autowired
    private TaskService activitiTaskService;

    @Override
    public void notify(DelegateTask delegateTask) {
        String lastAssignee = (String) delegateTask.getVariable("lastAssignee");
        if (lastAssignee != null) {
            List<Utilisateur> utilisateur = utilisateurRepositoryService.findByIdList(Lists.newArrayList(Long.valueOf(lastAssignee)));
        try {
            activitiTaskService.setAssignee(delegateTask.getId(), utilisateur.iterator().next().getId());
        } catch (ActivitiException ae) {
            throw new ProcessEngineException("Unable to assign task with taskId={} to user with userId={}.", ae,
                    delegateTask.getId(), utilisateur.iterator().next().getId());
        }

        }

    }
}

My 1st question is that am I wrong using the TaskListener to achieve what I want to do ?
2nd question, if I'm wrong is there another way to achieve this without using the TaskListener ?

Thanks for advices.
2 REPLIES 2

frederikherema1
Star Contributor
Star Contributor

String lastAssignee = (String) delegateTask.getVariable("lastAssignee");
        if (lastAssignee != null) {
            List<Utilisateur> utilisateur = utilisateurRepositoryService.findByIdList(Lists.newArrayList(Long.valueOf(lastAssignee)));
        try {
            activitiTaskService.setAssignee(delegateTask.getId(), utilisateur.iterator().next().getId());
        } catch (ActivitiException ae) {
            throw new ProcessEngineException("Unable to assign task with taskId={} to user with userId={}.", ae,
                    delegateTask.getId(), utilisateur.iterator().next().getId());
        }

Use the delegateTask.setAssignee() instead of using the service, this will set the field on the entity that is about to be persisted…

kse
Champ in-the-making
Champ in-the-making
Thanks a lot frederikheremans. It works. Smiley Happy