Building points system with sets and switches... easier way?

Check out below screenshot of workflow

Screenshot 2023-08-13 at 02.11.01

I am using a bunch of sets and switches to set scores based on past values. E.g. I want to add a different points to “points” variable depending on age… then I’ll do the same with something else…

Use case = lead scoring for CRM

Question: is there an easier way to achieve this in a code block or something using a switch statement inside of it? That’s how I’d do it if i was using “vanilla JS”

Many thanks :slight_smile:

Not sure about the exact usecase but you can probably make a list and then merge(combine - enrich input 1) that to your data. It depends on the data checks though.

From looking at your screenshot I suspect each Set node is indeed just adding or subtracting points? This would easily be doable in a Code node as well and is probably how I’d implement it seeing the number of different options.

Here’s a quick example workflow that adds or subtract points depending on an age value from an incoming item:

It simply uses vanilla JS along with n8n-specific variables like so:

if ($json.age < 10) {
  $input.item.json.score += 10;
} else if ($json.age < 20) {
  $input.item.json.score += 20;
} else if ($json.age < 30) {
  $input.item.json.score += 25;
} else if ($json.age < 40) {
  $input.item.json.score += 15;
} else if ($json.age < 50) {
  $input.item.json.score += 10;
} else if ($json.age < 60) {
  $input.item.json.score += 5;
} else if ($json.age < 70) {
  $input.item.json.score -= 5;
} else {
  $input.item.json.score -= 20;

return $input.item;

Adding to or subtracting from the score field based on the age value and the returning the following result:

Hope this helpsl


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