Tracking changes string

Hello,

I’m trying to track changes in data with this code

const staticData = getWorkflowStaticData('node');
const newStatusIds = items.map((item) => item.json['status']);
const oldStatusIds = staticData.oldStatusIds;

if (!oldStatusIds) {
  staticData.oldStatusIds = newStatusIds;
  return items;
}

const actualNewStatusIds = newStatusIds.filter((id) => !oldStatusIds.includes(id));
const actualNewStatuss = items.filter((data) =>
  actualNewStatusIds.includes(data.json['status'])
);
staticData.oldStatusIds = [...actualNewStatusIds, ...oldStatusIds];

return actualNewStatuss;

But for some reason this doesnt work, (It does work when ran manually so it must have to do with the fact that it’s not an ID but a string)
Does anyone know how to fix it or what’s wrong with the code? (This code does work with ID’s)

Without having looked at the code at all. Did you check it in production mode? Because running a workflow manually (when pressing “Execute Workflow”) does not save static data.

Here the documentation:

Yes, I see that i actually forgot to type that

It looks like you are using the data from “status” which contains strings like “open”. That would not work as multiple rows would have similar data. You would have to use a unique value like an ID or a date-time for it to work.

As the ID does not change but i need to track the status changes, how would i go about that?

Anyone knows a solution for this?

Hey @Damian_K!

I would store both the ID and status in an object. On every execution, I would then check for the status of an ID and if it got changed I would return it. I hope this helps.

I’ve been trying to do it like this to no avail

const newStatusIds = items.map((item) => item.json['action','status']);
const oldStatusIds = staticData.oldStatusIds;

if (!oldStatusIds) {
  staticData.oldStatusIds = newStatusIds;
  return items;
}

const actualNewStatusIds = newStatusIds.filter((id) => !oldStatusIds.includes(id));
const actualNewStatuss = items.filter((data) =>
  actualNewStatusIds.includes(data.json['action','status'])
);
staticData.oldStatusIds = [...actualNewStatusIds, ...oldStatusIds];

// Update its data
return actualNewStatuss;

Where as "action’ is the id and “status” well, is the status

Do you maybe have example code for this?

I don’t have an example code for this at the moment. I will try to build one and share it here.

That would be amazing!, Thanks!

@harshil1712 Did you have a chance to look at it? :smiley:

Hey @Damian_K! I took a quick look, couldn’t dive into it, and found out that your code isn’t comparing the status correctly. Let me try to explain the idea I had to solve this issue.

On every execution, we store the incoming data in an array. This array should contain objects with id and status. Our static data should also contain the data in a similar format. [{id:1, status:'open'}, {id:2, status:'close'}] something similar to this. We then check the status for each ID. If the status changes we return the new status or do nothing. Your code is not storing the data in the correct format, and not filtering it as needed. I hope this helps.

Yes that’s what i tried to do with the last example code i posted ['action','status'] where in “action” is the id, didnt work as how i expected

You got an update for me on this maybe?..

Hey @Damian_K!

I tried debugging your code. For me, the oldStatusIds is not getting updated with the newly modified data. That can be a reason that we are getting the desired output. I log the variables on the console to debug and understand the structure that they store. I did the same for your code and the structure is different from what we need. For example, newStatusIds only stores the status and doesn’t store the IDs. Is that intentional?
Another structure we can use is as follow { 1: 'Open', 2:'Close', ...}. Here, 1, 2 are the IDs and we are directly mapping the status to the IDs. Let me know if this makes sense.

No, This is not intentional, I’m an absolute dumbass regarding Javascript to be honest

Hey @harshil1712 I looked at the workflow you posted at https://n8n.io/workflows/864
if this indeed watches changes in the data, would this work for me perhaps?

and in the text you say “Update the Set node and the Function node accordingly.”
There is no set node