cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptTask + TimerBoundaryEvent

soma
Champ in-the-making
Champ in-the-making
Hi,

I have a simple Activiti diagram with a ScriptTask and a TimerBoundaryEvent.
You can see my diagram if you open this url: http://s22.postimg.org/ueb4mrsyp/My_Process.png

My first task is a ScriptEvent task. In this task (I use JavaScript) I would like to set a date variable. It represents the start time of the process. After ScriptEvent task I have some other tasks and a TimerCatchingEvent component. It is a TimerIntermediateCatchEvent. Here I want to reuse my predefined variable and set it as a timeDate property of my TimerCatchingEvent component.

Here is my script of the ScriptEvent task:

<scriptTask id="scripttask1" name="Set timer variable" scriptFormat="javascript" activiti:autoStoreVariables="true">
<script>
//var now = new Date();

// 5 minutes later
execution.setVariable("process_start_time", new Date((new Date()).getTime() + 5 * 60000) + "");

// 2 months later
//execution.setVariable("process_start_time", today + 60);</script>
</scriptTask>


My TimerCatchingEvent definition:

<intermediateCatchEvent id="timerintermediatecatchevent1" name="TimerCatchEvent">
<timerEventDefinition>
<timeDate>${process_start_time}</timeDate>
</timerEventDefinition>
</intermediateCatchEvent>


If I use Date type in my ScriptTask I get this persist exception: Couldn't serialize value 'sun.org.mozilla.javascript.internal.NativeDate@1b37a0d' in variable 'process_start_time'

In order to avoid serialization exception I convert the Date type to String with a litle trick: +"". It works fine.

But now I have another exceprion: couldn't resolve duedate: Invalid format: "Wed Jan 15 2014 14:33:48 GMT+010…". So my date format is not align to the expectation.

My questions are:
* How can I set a Date type variable with ScriptEvent task? If I use Date type I get a serializable exeption.
* How can I set dinamically the value of the timeDate parameter of TimerCatchingEvent component via ScriptEvent task.

If possible I would like to avoid to use java class for this easy function.

Thanks.
5 REPLIES 5

frederikherema1
Star Contributor
Star Contributor
the timeDate expects an ISO8601 date. You can also use the timeDuration-property instead and specify a duration. Eg. use "PT5H" to make the timer fire in 5hours of "P15D" to have the timer fire in 10 days.

soma
Champ in-the-making
Champ in-the-making
The timeDuration property is not fits to the requirements because:

(1) The timer event needs to appear x days after the workflow has been started to run.
and
(2) The timer event must not appear before the subprocess is done.

If the running time of the subprocess is longer then x days the timer event needs to appear immediately when the workflow is in the red point (please, have a look at my new image).

If the workflow reaches the red point before the x days then the timer needs to wait (start date + x days - now).

Image: http://s15.postimg.org/z5u5xx8bf/My_Process_02.png

In order to handle it I need to set dinamically the timeDate property. And I do not want to use java layer for this because somtimes the key user needs to modify the lenght of this time period (she redraw activiti diagram ) but they are not able to rebuild and redeploy the application.
Nevertheless the key users are able to modify and upload the new version of the workflow.

This is the reason why I would like to calculate the value of timeDate property via ScriptTask and I want to put Date variable to the context of the workflow instance.

Thx.

frederikherema1
Star Contributor
Star Contributor
Okay, so you need to make the ScriptTask convert to an ISO-8601 date, that is used in the timeDate. You can, for example, create a util-class for this in java and expose it as a "bean" in the process-engine configuration and use it in your script. This way, no java-coding needs to be done when the time to wait is modified, only the script.

soma
Champ in-the-making
Champ in-the-making
Thanks for your help!

soma
Champ in-the-making
Champ in-the-making
Finaly I created an groovy script. It is working very well and it is just 3 rows and it is very easy:

<code>
<scriptTask id="scripttask1" name="Set timer variable, time limit = 2 months" scriptFormat="groovy" activiti:autoStoreVariables="true">
   <script>def d = new GregorianCalendar()
                 d.add(Calendar.MINUTE,3)
                 execution.setVariable("process_start_time", d.getTime().format("yyyy-MM-dd'T'HH:mmZ"))
</script>
</scriptTask>
</code>

And this is my timer code;
<code>
    <intermediateCatchEvent id="timerintermediatecatchevent1" name="TimerCatchEvent">
      <timerEventDefinition>
        <timeDate>${process_start_time}</timeDate>
      </timerEventDefinition>
    </intermediateCatchEvent>
</code>



Thanks for your inspiration!