Assigning data based on predefined criteria

Describe the problem/error/question

Hello, first of all I’m not good at all with javascript but I did get some other stuff working with N8N nicely! It’s a great tool. Now I’m facing a usecase that I have no idea how to solve within N8N without using javascript.

I have data coming in from one side about users that chose their new hardware based on type.
The other branch has all of the available hardware with unique serial numbers.
The goal is to match new HW with the chosen type and go from the oldest.
So far I have a problem with assigning SN one after another as it always gives the same first match to everyone.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

[
  {
    "email": "[email protected]",
    "HW_ID": "100",
    "SN": "SN1",
    "purchase_date": "1.1.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "200",
    "SN": "SN3",
    "purchase_date": "1.2.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "200",
    "SN": "SN5",
    "purchase_date": "1.7.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "205",
    "SN": "SN6",
    "purchase_date": "1.1.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "200",
    "SN": "SN4",
    "purchase_date": "15.1.2023"
  }
]

Information on your n8n setup

  • n8n version: 0.236.3
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Danone :wave: Welcome to the community! :tada:

I’m getting a slightly different output to you running this on n8n 1.9.0:

[
  {
    "email": "[email protected]",
    "HW_ID": "100",
    "SN": "SN1",
    "purchase_date": "1.1.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "200",
    "SN": "SN3",
    "purchase_date": "1.2.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "200",
    "SN": "SN3",
    "purchase_date": "1.2.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "205",
    "SN": "SN6",
    "purchase_date": "1.1.2023"
  },
  {
    "email": "[email protected]",
    "HW_ID": "200",
    "SN": "SN3",
    "purchase_date": "1.2.2023"
  }
]

Is this more of what you expected? If not, can you let me know what you do expect so I can help you fix this up? :slight_smile:

Sorry, I’ve read that wrong. Yes that is my desired outcome

hi @Danone

The problem here is with the dates. As they are strings, then asc (and desc) sorting is not accurate with DD-MM-YYYY format. Example for ASC:
Date 01 Feb 2023 will be on the first place and 5 Jan 2023 will be on the second, as 01-02-2023 < 05-01-2023 because of 01 < 05. So February will be older because the day will be taken into account firstly. Sorting with strings is going from left to right one-by-one untill the first change, it doesn’t care about dates. You need the format YYYY-MM-DD to get it working. In that case:
2023-02-01 > 2023-01-05 because of 02 > 01. As you can see, the month will be taken into account firstly, instead of the day.

The other solution is to convert date string into date object, so the sorting could treat it as a number.

Hello @barn4k

thanks for the feedback. I can now see that this is also an issue.

Also I wasn’t clear enough of what my main issue is. Basically as @EmeraldHerald pointed out the data output assigns the first SN to each ID without skipping one that was already assigned. I assumed that merge node with Include first match only would skip the one that was already assigned.

I guess this cannot be resolved with nodes and requires using code.

I’ve reworked the flow. Now instead of finding out which S/N should be assgined I’m just calling each one after another to be assigned with a timed delay

2 Likes

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