Else if Statments in N8N

I am a new user to N8N and it is very user friendly but I have a scenario I cant get to work properly and it seems very simple.

I get info from a Webhook that contains a field “ServiceLevel”. This field cane be 1 of 5 different value. I need to trasnform this value based on the criteria below to a SET that contains a ton of other fields I need

What I am trying to do in a SET Expression, but it doesnt work

if($json.body.shipmentType=“LTL”){“FTL-LTL”}
else if($json.body.shipmentType=“Truckload”){“FTL-Truck”}
else if($json.body.shipmentType=“Domestic Freight”){“Airfreight”]
else{“Small Package”}

Hi @Shaundcmason - welcome to the community :tada:

Out of curiosity, have you taken a look at the If node and Switch node? Specifically the Switch node might help you out here :slight_smile:

I have tried the switch mode and I can make it work but I have to replicate all steps after that and wanted to keep the workflow simple. Is there a way to create a switch mode and have results concatenate back into single set based on the results of the Switch(0,1,2, etc)

@Shaundcmason can you share your workflow so we can get a better look at what you’re trying to do? That’d be helpful!

@Shaundcmason In the value field you have to use oneliners.
So your condition would look like this

{{ $json.body.shipmentType=“LTL” ? “FTL-LTL” : $json.body.shipmentType=“Truckload” ? “FTL-Truck” : $json.body.shipmentType=“Domestic Freight” ? “Airfreight” : “Small Package” }}

which is awful, and never should be used.

You would better be using Code Node, with this expression:

var shipmentType = $node["*Name of the node you are getting data from*"].json["body"]["shipmentType"];

 switch (shipmentType) {
         case "LTL": {
         var newShipmentType = "FTL-LTL" }
         case "Truckload": {
         var newShipmentType = "FTL-Truck" }
         case "Domestic Freight": {
         var newShipmentType = "Airfreight" }
         default: {
         var newShipmentType = "Small Package" }
                                       }

return {
  json:
  { 
     "newShipmentType": newShipmentType
  }
       }

Or just use a Switch node, that was suggested above.

1 Like

I have tried the below but no matter what the original value is it defaults to small pacjage as the result

var shipmentType = $node.TAI_Webhook_CreateShipment.json.body.shipmentType;

switch (shipmentType) {
case ‘LTL’: {
var newShipmentType = “FTL-LTL” }
case ‘Truckload’: {
var newShipmentType = “FTL-Truck” }
case “Domestic Freight”: {
var newShipmentType = “Airfreight” }
default: {
var newShipmentType = ‘Small Package’ }
}

return {
json:
{
“original”: shipmentType, “newShipmentType”: newShipmentType
}
}

I would share what im working on but I dont know the best way to do that

Instead of this

$node.TAI_Webhook_CreateShipment.json.body.shipmentType

Use this syntax

$node["TAI_Webhook_CreateShipment"].json["body"]["shipmentType"]

I appreciate all the help but for some reason it still is using th default, any other ideas?

Got it working it just needed the ;break value added

ar shipmentType = “LTL”;

switch (shipmentType) {
case “LTL”: {
var newShipmentType = “FTL-LTL” };break
case “Truckload”: {
var newShipmentType = “FTL-Truck” };break
case “Domestic Freight”: {
var newShipmentType = “Airfreight” };break
default: {
var newShipmentType = “Small Package” };break
}

return {
json:
{
“original”: shipmentType, “newShipmentType”: newShipmentType
}
}

2 Likes

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