Send only new data through workflow

I am polling today’s sales data from one sistem via API and I want to send a Slack notification for every new customer that made a purchase. Since I am polling every few minutes, the same “new” customer will be sent to Slack many times during the day.

I tried the function described in Creating triggers for n8n workflows using polling ⏲ – n8n Blog but it returns the following error:

Please share the workflow

Information on your n8n setup

  • **n8n version:**0.221.0

  • Database you’re using (default: SQLite): postgres

  • Running n8n with the execution process [own(default), main]: main

  • **Running n8n via [Docker, npm, n8n.cloud, desktop app]:**docker

Hi @fxholl!

I notice that the API you’re fetching the data from has the ability to filter by when the record was created, and that your cron is running every 5 mins.

In that case I would suggest setting the ‘created_at’ param in your HTTP node to

{{ $now.minus(5, 'minutes') }}

That way you’ll only be fetching records created after the last execution of the workflow.

1 Like

great suggestion , I will try that and report back.

It does not work because when i set the the ‘created_at’ param in the HTTP node to {{ $now.minus(5, ‘minutes’) }}, which sends something like 2023-03-24T11:23:22.228-03:00, the system just considers the date 2023-03-24 and returns all customers of the day.

Now it gives another idea: perhaps I can add a node to filter all dates after {{ $now.minus(5, ‘minutes’) }}

I now tried now with a different workflow and noticed the following syntaxt error:

I’d suggest reading up on how you use => functionality in the .map() function.

Essentially the thing before the arrow should just be a variable you define (e.g. x), then you use it after the arrow.

I don’t know JS at all. I tried to feed this to ChatGPT, and it suggested the following code:

< const new_items = [];

// Get static data stored with the workflow
const data = this.getWorkflowStaticData(“node”);

data.ids = data.ids || [];

for (let i = items.length - 1; i >= 0; i–) {

// Check if data is already present
if (data.ids.includes(items[i].json.ID)) {
	break;
} else {

	// if new data then add it to an array
	new_items.push({
		json: {
			name: items[i].json.name,
          phone_number: items[i].json.phone_number,
          purchase_value: items[i].json.purchase_value
		},
	});

	// Add the ID of the new item to the list of stored IDs
	data.ids.push(items[i].json.ID);
}

}

// return an object with the items property
return {
items: new_items
};

but I am still getting ERROR: this.getWorkflowStaticData is not a function [line 4]. Isn’t getWorkflowStaticData a valid function in n8n?
.

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