Conditionally set values

Hi!

I’m trying to build a workflow that involves setting the value of a variable based on some conditions. I have seen some previous posts like:

…and while they cover my question, I’m hoping to see if there’s a different solution than the ones I know or are proposed there.

What I’m looking for is basically an equivalent of:

// returning an object as n8n needs an object to be returned as far as I'm aware
switch (sp) {
  case 100001:
    return {
      l: 'string-1'
    }
  case: 100000:
    return {
      l: 'string-2'
    }
//  ... and so on
}

I am aware that the Code node can easily achieve this and that’s what I currently have, but my team is trying to have minimal dependency on the Code node for 2 reasons:

  • we want to have a no to low-code automation as much as possible. Yes, the above code does fall in the “low-code” category, but I figured I could ask if there’s a way to even further minimize it.
  • debugging the Code node as compared to a Switch node is a bit difficult because the Switch node highlights the exact condition and route the workflow took, which the Code node does not do. So with the Switch node, we can visually inspect what route the workflow took and then figure out why it might have done that.

When the above is translated to a Switch node, the workflow looks something like:

As you can see it looks really complicated. In my case that’s not even the complete workflow, so the n8n UI gets fairly laggy and the readability is completely lost. I had to trim down my condition count to upload the workflow in the editor. I have about 21-22 conditions in total.

Do I have any better options here?

There is a couple of ways to do this with an expression.
For example making an array with all possible input+outputs and then searching the array.
You can also do one object where the key is the input and value is the output. Then you can just inject the input as the key like:

$json.settingsobject[$json.valuetogetOutputFrom]

That’s… brilliant. Simple and effective. Thank you! I went with the object route. Added an Edit Fields node, set a key as an object and in the next Edit Fields, used the expression.

Would you also happen to have any such ideas for a Switch node where multiple conditons set the same value, for example:

I can create another object which has multiple keys with the same value, but the strings are rather long so I’d prefer avoiding it if possible.

EDIT: Sorry, it looks like it can be also solved with another Edit Fields node. I guess that’s good enough for now.

1 Like

Weirdly, I’m not seeing an option to mark your response as a solution. This is all I get:

that is because the category is not “questions“ :slight_smile:

1 Like

For the multiple going to the same value is going to be more tricky. Would have to have a think about it.
Probably easiest to map them to 1 value first and then do the normal object route.

For multiple values, I created something like this in an Edit Fields where a is the node in which I’m stroing all the longer strings.

{{ 
  {
    '100000': $('a').item.json.string_1,
    '10000': $('a').item.json.string_1,
    '1000': $('a').item.json.string_1,
    '999': $('a').item.json.string_1,
    '200': $('a').item.json.string_2,
    // and so on...
  }[$('sp').item.json.max_sp]
}}

looks complicated. Not sure what this would do. (is a long day for me already maybe that is the reason)

I would make one object with key/values to map all multiple keys to a single one. And then use the other object to simulate the switch statement like before.

Scratch that you dont even have to do that. You can just have the same value for multiple keys.:sweat_smile:

Nothing major, but unlike the original post, the strings are long (almost like a paragraph). Repeating the same string over and over in the same object would cause readability issues and also be a challenge when updating as we would have to update all instances of the string. With the paragraphs now stored in a single Edit Fields node, that part is handled. The other alternative was to store them in the same expression, but that seemed even worse (and not sure if that’s even possible), something like:

{{
  const foo = '...';
  {
    '10000': foo,
    '1000': foo
  }
}}

I’m open to ideas, but simply reading through this text, I’m unsure what you mean. Sorry, my comprehension might not be the best here :sweat_smile: If you have some time later, do you mind sharing an example?

For one-word strings, I’d have done that, for paragraphs that sounds less ideal.

So you want to check if a value is a whole paragraph?

Can you provide an example of the input and output data maybe? Some sample or whatever. Ill have a quick look tomorrow.

Thank you! The context here is that, this is kind of a backend system for our support tickets. For each ticket, we would like to:

  • fetch the user’s support priority from our database
  • set a level-tag based on it
  • send an auto-response as well

So for example, if a user has support priority of 100,000 we want to tag it level-2. If someone has a priority of 10,000, we want to tag it level-1 However, we want to send the same auto-response to both of these. The tag is small, the auto-response is a paragraph.

These are the tags with the support priority being the key:

{
  '100001': 'enterprise-escalation',
  '100000': 'level-2',
  '10000': 'level-1',
  '1000': 'level-0',
  '999': 'enterprise-trial',
  '200': 'orb-ind-business',
  '102': 'orb-osu-netlifriend',
  '101': 'enterprise-sandbox',
  '100': 'orb-business',
  '15': 'conduct',
  '12': 'orb-sponsored',
  '11': 'credit-pro',
  '10': 'orb-pro',
  '7': 'dmca',
  '6': 'credit-starter',
  '5': 'orb-starter',
  '4': 'gdpr',
  '3': 'abuse',
  '2': 'login',
  '1': 'free-is-free',
  '0': 'free-is-free'
}

As you can see, 1 and 0 have the same value for their level. This is possible because free-is-free is a much shorter value than the auto-response. Now from this list, we need to send the same autoresponse to: 100,000, 10,000, 1,000, 999, 101, then the next set is 200, 100 another set is 11, 10. Having entire set of paragraphs copy-pasted for each of these does not sound ideal especially because if we have to change it, we’ll have to find each instance of that paragraph and update it. It’s easy for me to remember if I’m working solo on this, but in a team, such mistakes are easy to occur, so trying to not have to repeat the paragraphs.