Set a timer relative to a date defined in process variable

Hi,

I am trying to define a timer that is triggered relative to a process variable. Say when the process starts we define a process variable appointmentTime. And we would like the process engine to send an automatic reminder 1 hour before the appointed time.

I tried using something like: ${dateTime(appointmentTime).minusHours(1).toDate()}. But that does not work. The timer definition seems only to accept either a fixed date or a period. What is the proper way to do datetime arithmetic in the timer definition?

Thanks,
Hong Yuan

1 Like

Hi Hong Yuan,

Can you please provide a unit test on github that reproduces the behavior? That makes it easiest for us to understand and reproduce the problem as well as making a suggestion for improvement.

Cheers,
Thorben

Hi Thorben,

Not being a Java programmer, I am using Camunda purely via the REST API. A unittest on github is beyond my capabilities. I will try to make my problem more explicit:

The online documentation says the following about defining a timer based on process variable:

Expressions
You can use expressions for the timer event definitions. By doing so you can influence the timer definition based on process variables. The process variables must contain the ISO 8601 (or cron for cycle type) string for the appropriate timer type.

This means, however, that the timer value must be fully pre-calculated by some previous tasks before the timer activity is reached.

Suppose now that in one user task the end user sets a doctor appointment time, which is saved in a process variable named appointmentTime. Now we would like the user to be reminded one day before the appointed time. We could calculate an additional process variable named timeToRemind when user submits the task. But I thought it should be easier by doing some time arithmetic in the timer definition itself.

In the Expression Language section of the online documentation something very close is mentioned for use in the definition of task due dates:

<userTask id="theTask" name="Important task" camunda:dueDate="${dateTime().plusDays(3).toDate()}"/>

So I thought whether it is possible to define the reminder timer likewise using Expression Language as:

<timerEventDefinition>
   <timeDate>${dateTime(appointmentTime).minusDays(1).toDate()}</timeDate>
</timerEventDefinition>

But as it turns out, the internal function dateTime only returns the DateTime object of the current date. It can not convert existing process variable to Joda-Time DateTime object.

So actually my question boils down to:

Is it possible to perform datetime arithmetic, using the Expression Language, on a date stored in a process variable, for use in the timer definition?

Any suggestion is welcome.

Regards
Hong Yuan

Hi Hong Yuan,

Thanks for the clarification, now I understand the issue better. I don’t think there is a one liner that you can write in EL to express this logic. I would recommend using a script-based input mapping (probably Groovy) on the timer event. In that script you could transform the date in any way you like, for example using the Joda Time classes or Java 8’s classes for date and time manipulation.

Cheers,
Thorben

@Yuan_HOng based on the API of JodaTime, have you tried to convert your appointmentTime to milliseconds: http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#DateTime-long-

Taking the hint from Stephen and after painful testings, I was finally able to come up with a one liner which does the job, though a very ugly one :slight_smile:

${dateTime().plusMillis(appointmentTime.getTime()-now().getTime()-24*3600*1000).toDate()}

Don’t if there is any simpler solution.

1 Like

it’s an old thread, but for others like me searching for this problem.

at the End my solution was:

${dateTime().withMillis(appointmentTime.time). plusDays(1).toDate()}

1 Like