cancel
Showing results for 
Search instead for 
Did you mean: 

RuntimeService.setVariable

b_schnarr
Champ in-the-making
Champ in-the-making
Hello at all,

I have a question concering the Activiti Java-API RuntimeService-method

void setVariable(String executionId, String variableName, Object value) 


you can set variables for a corresponsing task, that is clear. But I do not understand how Activiti determines the datatype of the variable. Lets say I have a duedate (type date), or a boolean or a decimal….When I use this method, I simply pass an object variable. How does activiti know the datatype of the variable?

Thanks and best regards
Ben
7 REPLIES 7

b_schnarr
Champ in-the-making
Champ in-the-making
I want to realize an external form rendering with xForms. I read several threads about it and the documentation. I cannot figure out how to realize the mapping between process variables and Xform data.

Lets say I have a process and this process should use 3 process variables: assignee (string), duedate (date) and a boolean value called test.
Now I define a corresponding xform with 3 fields and they have the same name as the process variables: assignee, duedate and test.

The xForm submits the data as XML to a servlet and I need to process the XML in the servlet to pass the variables to Activiti. The question is how to do this.

When I use <code>void setVariable(String executionId, String variableName, Object value)</code>
Activiti does not know that it is a string, or a date or a boolean.
Therefore my question: how can I update already existing process variables (or task variables) so that the datatypes are clear from the outside (e.g. from a servlet). For that use case, should I use process variables or form properties or data objects?

Thank you very much and best regards
Ben

jbarrez
Star Contributor
Star Contributor
> How does activiti know the datatype of the variable?

The process engine configuration has a list of VariableTypes. When a variable is passed, the correct one is used using the class of the objec to serialize/deserialize. This mechanism is pluggable.

> The xForm submits the data as XML to a servlet and I need to process the XML in the servlet to pass the variables to Activiti

Everything in the XML is a string. So I assume Xform does add a type indication to it?
Use that type to create the correct java object before you pass it in the service.

> should I use process variables or form properties or data objects

Imho process variables are way more flexible than form properties/data objects.



b_schnarr
Champ in-the-making
Champ in-the-making
Lets say I have already defined a variable called dueDate during the modeling phase in my process definition.
When I then pass a variable with the same name (dueDate) as an object with this method
<code>void setVariable(String executionId, String variableName, Object value)</code>

then the variable already exists. Shouln´t activiti be aware of the datatype since I already specified it in the process definition?

How can I define a process variable during modeling time? I thought this is realized with data objects. Because I need to define them during modeling phase so that I can use them in gateways etc. The documentation says "The data object definitions will be automatically converted to process variables using the 'name' attribute value as the name for the new variable"

b_schnarr
Champ in-the-making
Champ in-the-making
I defined a bool data object with id = test, name = Test datatype = boolean, and default = false
I use this expression in a gateway condition: ${Test == 'true'}

When I update this data object in my servlet with
<code>processEngine.getRuntimeService().setVariable(task.getExecutionId(), "Test", "true");</code>
This works fine, even though I did not pass a bool java object….I do not understand why this works.

It seems like the datatype in the servlet does not matter when you already defined this data object with datatype in the process definition..Is this right? I also tested with the type date. It also works with

<code>processEngine.getRuntimeService().setVariable(task.getExecutionId(), "dueDate", "15/12/1989");</code>

Even though "15/12/1989" is a String and dueDate is a data object with type date.
Thank you very much
Ben

trademak
Star Contributor
Star Contributor
Hi Ben,

The Activiti Engine doesn't check the variable type. So if you update a boolean variable with a String it just will update the value to a String.

Best regards,

b_schnarr
Champ in-the-making
Champ in-the-making
Strange, it seems to work with form properties. I defined a form property isOK as boolean. The explorer (and the engine) have recognized it, because in the explorer, a checkbox is displayed instead of a text field.
Now, when I update this variable in a servlet using <code>processEngine.getRuntimeService().setVariable(task.getExecutionId(), "isOK", "true");</code>
the datatype remains boolean and in the Activiti Explorer, the checkbox is activated. In addition, ${isOK == true} is true.

This is great but I do not understand, how this can be? Because I passed "true", and this is a string.

And the same is true vor a date.

With <code>processEngine.getRuntimeService().setVariable(task.getExecutionId(), "myDate", "15/12/2014");</code>, the variable gets updated and the Explorer still sees it a a date, because it shows me the calendar widget.
That´s great, because it seems that the engine automatically choses the pre-defined datatype.

trademak
Star Contributor
Star Contributor
Form properties are implemented in a different way. When a form property is defined as a boolean or date type it will try to parse a string value to a boolean or data object. So this is not related to how it is stored in the database.

Best regards,
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.