Is there a way to simplifiate redondante switch case?

Hi, in my workflow, I need to add information to certain redundant fields depending on the value of a field. I would like to know if there’s an easy way to do this using only one node that contains all the cases and the fields to fill in as JSON.
Something like this, for example:

{
“cases”: [
{
“match”: “PLANTING RISK (case 1)”,
“status_gpt”: “PLANTING RISK”,
“priorite”: “High”,
“actions”: “-Check the information in the TMS -Call the carrier and if there’s no answer, follow up by email”
},
{
“match”: “PLANTING RISK (case 2)”,
“status_gpt”: “PLANTING RISK”,
“priorite”: “High”,
“actions”: “-Check the information in the TMS -Call the carrier again and start looking for a solution”
},
{
“match”: “AWAITING CARRIER RESPONSE (case 1)”,
“status_gpt”: “AWAITING CARRIER RESPONSE”,
“priorite”: “Low”,
“actions”: “-Monitor and follow up with the carrier in 30 minutes if no reply -Mark as ‘Urgent’ in case of follow-up”
},
{
“match”: “AWAITING CARRIER RESPONSE (case 2)”,
“status_gpt”: “AWAITING CARRIER RESPONSE”,
“priorite”: “Medium”,
“actions”: “-Check the information in the TMS -Follow up with the carrier by email or via the TMS if site arrival is not confirmed in the TMS”
}
]
}

As of now, most of my switch cases look like this (or even bigger, with more than 8 states), and it would be much more maintainable if I could change everything in a single entry.

Could you elaborate on this, please? An example maybe?

welcome @Dominos_pizza to the :n8n: community!

You can handle this in a single Code node instead of building a huge Switch
something like this:

Thanks a lot, this is exactly what I was looking for!
I have a second question though do you know if it’s possible to create in n8n a structure where a user with no programming experience wouldn’t have to touch the code?

I imagine fields where you could create a new entry/object that includes the three fields: status_gpt, priorité, and actions, so the user could fill them in, and it would be taken into accont accordingly. Also the user could destroy an item.

I want to know if it’s possible to do a structure then do a list of item using this structure then using this list in the Code node

Yes @Dominos_pizza it’s possible,
if I understand you correctly, you want to separate match and cases into different node, like using a Set node, for easier manipulation.

here is an example

Hope this helps! If everything looks good, please mark this reply as the answer.

1 Like

Thanks a lot ! I think this should make it yeah :+1:

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