Teneo Developers Community

Branch on date: before/after

This question came up already about datetime so I’d like to document it here.

The use case is prompt the user for a date, then branch depending on before or after that date.

The first thing is to follow the instructions to add the datetime module to your solution. We show that here: https://www.teneo.ai/studio/extensions/datetimehandler

You can build this in four basic steps resulting in something like this for a branch on dates before Jan 1, 2000.

The prompt and branch:

The transition listener:

The flow variables:

These steps describe more in detail what you have to do:

  1. In the output that prompts for the date, you have one leaving transition with the condition %DT_DATE.PHR. The transition ends at a junction.

  2. In the same transition you define a listener to get the date expression out of the input.

In the condition field: %DT_DATE.PHR^{dateExpression=lob.date}

  1. The listener’s script should intepret the date:

interpretedDate = datetime.Handler.interpret(dateExpression, 'forward') simplifiedDate = interpretedDate.date.start[0]

  1. At the branch you can use the groovy function isBefore() to compare the entered date to a constant you define (cutoffDate).

Thanks for the explanation, Fred! That is super helpful!

I am currently testing the bot and it processes the date - but with errors. I have the following three questions here:

  1. If I want to write a precondition that the date shall be between today and a day in the past, say 15.01.2015, do I write isAfter(2015-01-15) or isBefore(2015-01-15)?

  2. If I want to write a precondition that the shall be between to dates in the past. How do I combine these two conditions?

  3. Do I need to integrate an option, which has no date condition?

Your help is very much appreciated! Many thanks!

Hi michristian,

I have built a similar flow to answer your questions.

  1. You would use .isAfter(2015-01-15), but that would not be sufficient. In order to ensure that the date in question is between the past date and today, you also have to restrict it with respect to future dates. In order to include “today” you would specify a range between the past date and “tomorrow”.

This is how my flow looks like:

“getDate”- transition contains:
%DT_DATE.PHR^{dateExpression=lob.date}

Script node (“interpret date”) contains:
interpretedDate = datetime.Handler.interpret(dateExpression, 'forward') simplifiedDate = interpretedDate.date.start[0]

“After past Date, before tomorrow” contains:
{simplifiedDate.isAfter(pastDate) && simplifiedDate.isBefore(tomorrow)}

Variable definitions for “pastDate” and “tomorrow” look like this:

image

  1. In order to have a condition for two dates in the past, simply add another variable (e.g. pastDate2) similar to pastDate in my example but with a different date.

  2. You may either want to add such an option and loop back to the bot asking for a date again, or, you may want to use re-visitable output nodes and/or a resume prompt for this. Find more information here.

Hope this helps!

Cheers,

Fabienne

BTW if you still get errors, feel free to post the error messages here as well!

Many thanks, Fabienne! I will give this “&&” connector a try. That looks like the solution I was looking for.

Cheers, Mira

1 Like