Adding tasks to cases


#1

Hi

I’ve found from various posts and code how to create a task - but just wondering how would i attach it to a particular case instance?

    Task t  = cmmnTaskService.newTask();
    t.setName("Something new");
    List<Task> myCurrentTasks =  
   cmmnTaskService.createTaskQuery().caseDefinitionId(caseInstance.getCaseDefinitionId()).list();
    myCurrentTasks.add(t);
    cmmnTaskService.saveTask(t);

I’m not quite sure what the “add” does - I saw this on another post but there doesn’t seem to be much different here.

Can I assume that with just the ‘saveTask’ I can use this for assigning tasks to various individuals - but they are totally outside of the case instances that they might be involved in?

Thanks
Chris


#2

Hi Chris,

The easiest way how to create a case task is to define sentry in the cmmn model which will be used to enter the task. In that case the task is created in the case context with all scopeIds set.
As an cmmn example see

and test

To add “adhoc” tasks to the case you have to set scope Ids properly.

Regards
Martin


#3

Hi @martin.grofcik

Thanks for this - I was actually after the ad-hoc side of things. Thanks for the pointer in terms of scope IDs. I’ve tried a few things this morning but I’m hitting a SQL syntax error - so presumably some field I’m not setting that then gets caught in the prepared statement?

//scope id is the case instance id
//subscope id ? stage? it’s the id of the plan instance (from runtime service) - but can’t be set it through the builder for some reason?
TaskBuilder taskBuilder = cmmnTaskService.createTaskBuilder();
TaskBuilder tb = taskBuilder.name(“Some name to the task.”).scopeId(caseInstance.getId()).id(“myid”).scopeType(ScopeTypes.CMMN);
Task task1 = tb.create();
cmmnTaskService.saveTask(task1);

I’m getting the following error:

Error updating database. Cause: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement: update ACT_RU_TASK
SET REV_ = ?,
NAME_ = ?,
PRIORITY_ = ?,
CREATE_TIME_ = ?,
SCOPE_ID_ = ?
SCOPE_TYPE_ = ?
SUSPENSION_STATE_ = ?,
IS_COUNT_ENABLED_ = ?,
VAR_COUNT_ = ?,
ID_LINK_COUNT_ = ?,
SUB_TASK_COUNT_ = ?
where ID_= ?
and REV_ = ?

Any pointers on where to go?

Is the scope ID always the case instance id? or would it be the stage depending on where the equivalent insertion place in the case is going to be?

Cheers
Chris


#4

Hi Chris,

I did something similar in the UI flowable-task app. Create task in the scope of case.

       TaskEntity task = (TaskEntity) taskService.newTask();
    task.setName(taskRepresentation.getName());
    task.setDescription(taskRepresentation.getDescription());
    task.setParentTaskId(taskRepresentation.getParentTaskId());
    task.setScopeId(taskRepresentation.getScopeId());
    task.setScopeType(taskRepresentation.getScopeType());
    if (StringUtils.isNotEmpty(taskRepresentation.getCategory())) {
        task.setCategory(taskRepresentation.getCategory());
    }
    task.setAssignee(taskRepresentation.getAssignee() != null ? taskRepresentation.getAssignee() :     SecurityUtils.getCurrentUserId());
taskService.saveTask(task);

branch:

Regards
Martin