Using dataObject in ScriptTaskListener

Hello to all,

I am trying to create dataObject that will create some script to be executed on user task. The idea behind is to avoid duplication of code, because I will have several user Tasks that will need to execute the same script, setting the same task local variables.
According to documentation data objects are converted to process variables, and I though that I could use the content of the process variable as script to be executed.
In xml definition I have

        <dataObject itemSubjectRef="xsd:string" id="dataObjectTest" name="dataObjectTest">
            <extensionElements>
                <flowable:value>
                <![CDATA[
                    task.setVariableLocal("variable1", "value1");
                    task.setVariableLocal("variable2", "value2);]]>
                </flowable:value>
            </extensionElements>
        </dataObject>

In user task

        <userTask id="myTaskId" name="My task">
            <documentation>Documentation</documentation>
            <extensionElements>
                <flowable:taskListener event="create"
                                       class="org.flowable.engine.impl.bpmn.listener.ScriptTaskListener">
                    <flowable:field name="script">
                        <flowable:expression><![CDATA[${dataObjectTest}]]></flowable:expression>
                    </flowable:field>
                    <flowable:field name="language" stringValue="JavaScript"/>
                </flowable:taskListener>
            </extensionElements>
        </userTask>

I tried almost all combinations for setting script with expression or string elements and I always get the exception

nested exception is org.flowable.common.engine.api.FlowableException: Exception while invoking TaskListener: Exception while invoking TaskListener: problem evaluating script: <eval>:1:1 Expected ; but found {
${dataObjectTest}
 ^ in <eval> at line number 1 at column number 1] with root cause

jdk.nashorn.internal.runtime.ParserException: <eval>:1:1 Expected ; but found {
${dataObjectTest}
 ^

I don’t know it this is good idea at all or maybe I should look for some other solution but I was following the example:

https://github.com/flowable/flowable-engine/blob/982be7e47facf17bb779571ba663bdf8f86947a3/modules/flowable-engine/src/test/resources/org/flowable/engine/test/bpmn/multiinstance/MultiInstanceTest.testParallelUserTasksCustomCollectionExpressionExtensionDelegateExpression.bpmn20.xml

Thank you!

Hi Ficus.

The question is: “How to access process variables in the ScriptTaskListener?”

		<userTask id="usertask1" name="All your base are belong to us">
			<extensionElements>
				<activiti:taskListener event="create" class="org.flowable.engine.impl.bpmn.listener.ScriptTaskListener">
					<activiti:field name="script">
						<activiti:string>
            	task.setVariable('foo', task.getProcessVariables().get('var')); // pushes variable to execution context
            </activiti:string>
					</activiti:field>
					<activiti:field name="language" stringValue="groovy" />
				</activiti:taskListener>
			</extensionElements>
		</userTask>		

Check whether process variables are set on the task.

Regards
Martin

Hi Martin,

Thank you for answering.
Process variable is already set, I checked the db. My problem is that I want to use content of process variable to be executed as script. The reason why I am doing this is to create smaller xml, I am implementing decision tree with flowable and some nodes are similar, I have userTasks where I set completely the same local variables. Maybe I should try with common delegate if this is not possible…

Best regards

Hi,

ScriptTaskListener does not support “script expression”. If you want to get a support for that, add ExpressionManager evaluation to ScriptTaskListener class.
Currently there is only support for the plain text:

Object result = scriptingEngines.evaluate(script.getExpressionText(), language.getExpressionText(), delegateTask, autoStoreVariables);

Regards
Martin