Dynamic parameter value from last execution timestamp

Hi! I am setting up a workflow where I need to dynamically set a property of an HTML request node which takes timestamps as a value. I was able to use for example {{new Date().toISOString()}} to set the current date time, but I need to reference somehow the timestamp of the previous successful execution in order to always fetch new data.

Thank you
Thanos

Hi @Thanos_Daskalopoulos, welcome to the community!

A possible approach would be to store the past execution time along with the workflow data at the end of your workflow using the Function node.

The downside to this approach is that this data is only available in production executions and not when testing a workflow through the UI. If you need this to also work for manual executions through the UI you’d need to store the time outside of n8n, for example in an external database.

thanks! In the function node docs I see how to access the static data with getWorkflowStaticData. Is there something similar for the execution data?

If I understand correctly the saving part would be done by the environmental variables config.(?)

So the data will be stored with the workflow, but it will be updated (and read) during the workflow execution.

So when running the example code from the documentation:

const staticData = getWorkflowStaticData('global');
const lastExecution = staticData.lastExecution;
staticData.lastExecution = new Date().getTime();

The lastExecution field would be updated whenever the workflow executes.

Here’s a quick example putting this all together and doing nothing except reading and writing this data:

Example Workflow

You can copy these example Function nodes into any of your workflows as needed, no environment variables would be needed.

The only thing to keep in mind is that they won’t return any data when manually running your workflow. So when testing this you’d need to trigger a production execution and then look up the data via the executions list:
image

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.