Complete task with variables

cancel
Showing results for 
Search instead for 
Did you mean: 
Member II

Complete task with variables

Hi, 

I'am using the Rest API in the Community edition and would like to achieve the following:

Complete a task and pass along variables which should be stored in local scope (task scope).

I use the runtime/tasks/{taskId}

with action=complete.

The doc says that a variable passed with the request is stored in global scope unless it exists a local variable which in that case is overriden. So, I first create a local variable on the task with same name and then pass along a variable with that name with the complete request.

Anyhow, the variable in the complete request is stored in global scope for some reason.

Have I misunderstood the API? How can I add local variables when completing a task?

5 Replies
Highlighted
Intermediate

Re: Complete task with variables

Before we answer the question, why are you trying to save a variable to "local" scope on task "complete".

Local scope means the variable exists only until the current task is completed. Hence adding a variable on the complete event is likely too late to be included in local scope.

What is it you are actually trying to achieve?

Greg

Highlighted
Member II

Re: Complete task with variables

For historical reasons. Want to attach a piece of information to be available for a task in historical perspective. Valid use case I think or is it a better way better doing this?

"Too late" you say, meaning that variables are not available for historical tasks or what?

I also would like to know if the api is functionally correct or not.

Thanks

Highlighted
Intermediate

Re: Complete task with variables

In order to make sure I can properly answer your, is it working correctly question. Please provide a unit test demonstrating what you are seeing.

Greg

Highlighted
Established Member II

Re: Complete task with variables

Hi Magnus Hellström ,

I also would like to know if the api is functionally correct or not.

The API should work as expected. 

POST request to: http://localhost:8080/activiti-rest/service/runtime/tasks/{taskid}

With a request body such as:

{
    "action":"complete",
    "variables": [
        {
            "name":"variablename",
            "value":"variablevalue"
        }
    ]
}

Also remember to design your process variables and task variables correctly. This docs (Activiti User Guide) should be helpful to understand process variables versus task/execution or local variables.

"Too late" you say, meaning that variables are not available for historical tasks or what?

It means that when you complete the task, the local variables are gone. There's no point saving them anymore. From the docs Activiti User Guide : 

Variables can also be fetched again, as shown below. Note that similar methods exist on the TaskService. This means that tasks, like executions, can have local variables that are alive just for the duration of the task.

You said

So, I first create a local variable on the task with same name and then pass along a variable with that name with the complete request.

Anyhow, the variable in the complete request is stored in global scope for some reason.

It's unclear to us what you are trying to do here so a unit test should demonstrate better your problem and therefore people can provide better answers. That's why in this forum, we recommend people to write clear questions with unit tests for demonstration. 

You said something about 'historical reasons', is Activiti's history service (Activiti User Guide ) not satisfying your requirements? Again, it's always a good idea to explain your use case before jumping to the technical part.

Hope this helps.

Thanks,

Thong Huynh

Highlighted
Member II

Re: Complete task with variables

In the TaskService there is a method 

complete(String taskId, Map<String,Object> variables)

and a overloaded version

complete(String taskId, Map<String,Object> variables, boolean localScope)

The overloaded version fits my requirements. The java doc of the method describes the purpose of the method well. When checking the source code of the Rest implementation, I can see that the complete method with no localScope parameter is called. 

Thanks anyway.