Clear Candidate Groups in User Task listener

Hi,

I’m trying to execute some authorization logic inside an User Task Listener (Create event) implemented through a CDI bean.
The task has a default candidate group; but if when it’s created, a particular process variable is set, the default candidate group should be removed and the task should be visible only to a list of candidate users fetched from an external service.

I tried with the following two pieces of code:

public void resetCandidates(DelegateTask task) {
authorizationService.createAuthorizationQuery().resourceId(task.getId()).list()
.forEach(a → getAuthorizationService().deleteAuthorization(a.getId()));
//fetch and set candidate users
}

public void resetCandidates(DelegateTask task) {
task.getCandidates().forEach(c → {
String groupId = c.getGroupId();
if (isNotBlank(groupId)) {
getTaskService().deleteCandidateGroup(task.getId(), groupId);
}
});
//fetch and set candidate users
}

but none of them had the expected effect (the candidate group is still there)…
I supposed the problem were related to the transaction not yet committed.
I would ask you if you know a better approach to this problem?

Regards,
Piero

Did you find any solution for this?