How to set current date as a new variable in task execution listener?

Hi! Usually-business-but-tech-curious user here, please be kind.

I need to calculate how long it takes from the start of Task1 to the end of Task2.

I got stuck in the first step which is using task execution listener with that setup:
image
and this script execution.setVariable("dataSolicitacaoExame",now()) to register when the first task starts.

This is the error I get:
Cannot instantiate process definition SimuladorTestesProcess:6:52202cec-f8b2-11ec-9f19-e4a8dff3b940: Unable to evaluate script while executing activity 'InsereVariavelTask' in the process definition with id 'SimuladorTestesProcess:6:52202cec-f8b2-11ec-9f19-e4a8dff3b940':org.graalvm.polyglot.PolyglotException: ReferenceError: now is not defined [ start-instance-error ]

Tips on how to overcome this first step that went wrong already? :joy:

Camunda internally records that info.

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.17/org/camunda/bpm/engine/management/Metrics.html

The first two metrics might be of interest to you.

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.17/org/camunda/bpm/engine/management/Metrics.html#ACTIVTY_INSTANCE_START

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.17/org/camunda/bpm/engine/management/Metrics.html#ACTIVTY_INSTANCE_END

You should be able to capture those metrics and determine the time between them.

Hope this helps.

Hi @Neeniih,

another view on the data that the engine collects come from the history service.

A good start could be this API call with a given processInstanceId as query parameter: Get Historic Activity Instances | docs.camunda.org

Hope this helps, Ingo

@htaylor @Ingo_Richtsmeier

I should mention that I not only need to calculate how long it takes from one point to another but I also need to set an score for the resulting time.

I thought about doing that by using a DMN but to do so I need both dates as variables.

Not sure how to connect that with what you’ve provided me.

Try this to get the current datetime

execution.setVariable("dataSolicitacaoExame", "" + new Date());