Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom variables in an Activiti task

Tags:

activiti

Problem

I have several Activiti task types, the base types are ServiceTask and UserTask.

Some ServiceTask types are Java implementations, each of them different. Then there are others.

I'd like to distinguish them in my code.

Question

Is it possible to store some custom attributes / properties in the tasks and if so, how?

Example: A ServiceTask that implements simple Logging functionality should have a property "TYPE" with the value "LOGGING" which I can then query later.

I tried using FieldExtension, but when I run the workflow, Activiti throws the error:

org.activiti.engine.ActivitiIllegalArgumentException: Field definition uses unexisting field 'TYPE'

Same happens for CustomProperty.

Of course I could solve that by declaring the field in the Java class, but I need a solution that works also for non Java service tasks and for user tasks. Or better yet if possible for all flow elements.

Thank you very much!

like image 907
Roland Avatar asked Nov 25 '25 04:11

Roland


1 Answers

You can define fields in XML/Designer:

<serviceTask id="javaService"
    name="Java service invocation"
    activiti:class="org.activiti.examples.bpmn.servicetask.ToUppercase">
    <extensionElements>
      <activiti:field name="text" stringValue="Hello World" />
  </extensionElements>
</serviceTask>

If you want to access the field in Java code you can do it like that:

public class ToUppercase implements JavaDelegate {
    Expression text;

  public void execute(DelegateExecution execution) throws Exception {
    String value = (String) text.getValue(execution);
    value = value.toUpperCase();
    execution.setVariable("upperedText", value);
  }

}

If you want to access variables in UserTask you have to implement TaskListener instead of JavaDelegate.

like image 139
Piotr Korlaga Avatar answered Nov 28 '25 15:11

Piotr Korlaga