Order Submit - Multiple Order Lines

Hi, I am very new to this and am trying to figure out, how can I do an HTTP Request to an OrderSubmit endpoint where the number of order lines is a variable array. The Order level mappings are one-to-one so they are straightforward but how do I map the order lines so that when there are multiple order lines they just keep adding to the OrderLines array? Any help will be greatly appreciated.

Required order submit json format:

{
    "Order": [
        {
            "InvoiceCode": "ABC001",
            "OrderDate": "2021-10-05T15:04:43.877",
            "OrderLines": [
                {
                    "ProductCode": "PRODUCT001",
                    "Description": "Product Description 1",
                    "Quantity": 250,
                    "Amount": 100.0
                },
                {
                    "ProductCode": "PRODUCT002",
                    "Description": "Product Description 1",
                    "Quantity": 250,
                    "Amount": 100.0
                }
            ]
        }
    ]
}

Current attempt at mapping which works but only for the first order line:

{
    "Order": [
        {
            "InvoiceCode": "{{$node["Order Webhook"].json["body"]["Order"][0]["InvoiceCode"]}}",
            "OrderDate": "{{$node["Order Webhook"].json["body"]["Order"][0]["OrderDate"]}}",
            "OrderLines": [
                {
                    "ProductCode": "{{$node["Order Webhook"].json["body"]["Order"][0]["OrderLines"][0]["ProductCode"]}}",
                    "Description": "{{$node["Order Webhook"].json["body"]["Order"][0]["OrderLines"][0]["Description"]}}",
                    "Quantity": {{$node["Order Webhook"].json["body"]["Order"][0]["OrderLines"][0]["Quantity"]}},
                    "Amount": {{$node["Order Webhook"].json["body"]["Order"][0]["OrderLines"][0]["LineNett"]}}
                }
            ]
        }
    ]
}

Hey @Suratwala,

Welcome to the community :sparkling_heart:

Did you try creating the JSON in the Function node? In the Function node, you can loop through the incoming data, create the JSON, and pass it to your HTTP Request node.

Let us know if you need more help :slight_smile:

Hi @harshil1712,

Thanks for your response. No I have not tried it and not even sure where to start. Could you provide a simple example based on the code I provided?

1 Like

Hey @Suratwala,

Here’s an example workflow that might help:

{
  "nodes": [
    {
      "parameters": {
        "operation": "getAllPeople",
        "limit": 3
      },
      "name": "Customer Datastore",
      "type": "n8n-nodes-base.n8nTrainingCustomerDatastore",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const OrderLines = [];\nfor (item of items) {\n  OrderLines.push({json:{ProductCode: item.json.id, Description: item.json.notes}})\n}\n\n// You can write logs to the browser console\n\nreturn OrderLines;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={\n    \"Order\": [\n        {\n            \"InvoiceCode\": \"ABC001\",\n            \"OrderDate\": \"2021-10-05T15:04:43.877\",\n            \"OrderLines\": {{$json}}\n        }\n    ]\n}"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    }
  ],
  "connections": {
    "Customer Datastore": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you so much for your help @harshil1712. I managed to ge that sorted but now I have some more questions :slight_smile: , hope you can assist.

  1. How do I rename a key in an array? - I have “ProductCode” coming in but when I submit, I need to use “StockItemCode”. I can do this for a single item, but not sure how to do it for all items in the array.

  2. How do I rename and insert a key that’s available at the header level into each line item - I need the RequiredDateTime within each item as well and it needs to be called RequiredDate and not RequiredDateTime.

Please see sample workflow attached:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        900,
        350
      ]
    },
    {
      "parameters": {
        "keys": {
          "key": [
            {
              "currentKey": "=Orders[0].Items[0].ProductCode",
              "newKey": "=Orders[0].Items[0].StockItemCode"
            }
          ]
        }
      },
      "name": "Rename Keys",
      "type": "n8n-nodes-base.renameKeys",
      "typeVersion": 1,
      "position": [
        1350,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n    \"json\": [{\n        \"Details\": {\n            \"TotalItemCount\": 1,\n            \"Items\": [{\n                \"ID\": 2485,\n                \"OrderNo\": \"2500\",\n                \"OrderDate\": \"2021-10-06T15:00:00+12:00\",\n                \"InvoiceCustomer\": {\n                    \"Code\": \"INTERNAL\"\n                },\n                \"DeliveryCustomer\": {\n                    \"Code\": \"INTERNAL\"\n                },\n                \"RequiredDateTime\": \"2021-10-10T15:00:00+12:00\",\n                \"OrderLines\": [{\n                        \"ProductCode\": \"PRODUCT001\",\n                        \"Description\": \"Test Product 1\",\n                        \"Quantity\": 45,\n                        \"Value\": {\n                            \"Net\": 45,\n                            \"Tax\": 6.75,\n                            \"Gross\": 51.75,\n                            \"TaxDetails\": {\n                                \"Code\": \"01\",\n                                \"Rate\": 15\n                            }\n                        }\n                    },\n                    {\n                        \"ProductCode\": \"PRODUCT002\",\n                        \"Description\": \"Test Product 2\",\n                        \"Quantity\": 45,\n                        \"Value\": {\n                            \"Net\": 45,\n                            \"Tax\": 6.75,\n                            \"Gross\": 51.75,\n                            \"TaxDetails\": {\n                                \"Code\": \"01\",\n                                \"Rate\": 15\n                            }\n                        }\n                    }\n                ]\n            }]\n        }\n    }]\n}];\n"
      },
      "name": "Sample Order Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1050,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "const Header = [];\nfor (item of items) {\n    Header.push(item.json[0].Details.Items)\n}\n\nconst Items = [];\n\nvar max = item.json[0].Details.Items[0].OrderLines.length\nfor (let i = 0; i < max; i++) {\n    Items.push(item.json[0].Details.Items[0].OrderLines[i]);\n}\n\n//Items.push(item.json[0].Details.Items[0].RequiredDateTime);\n\nreturn items.map(item => {\n    return {\n        json: {\n            \"Orders\": [{\n                \"InvoiceCustomer\": item.json[0].Details.Items[0].InvoiceCustomer,\n                \"DeliveryCustomer\": item.json[0].Details.Items[0].DeliveryCustomer,\n                \"OrderDate\": item.json[0].Details.Items[0].OrderDate,\n                \"RequiredDate\": item.json[0].Details.Items[0].RequiredDateTime,\n                Items\n            }]\n        }\n    }\n});\n"
      },
      "name": "Format Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1200,
        350
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Sample Order Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Order Data": {
      "main": [
        [
          {
            "node": "Format Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Data": {
      "main": [
        [
          {
            "node": "Rename Keys",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hey @Suratwala,

Here’s the example code that you can refer:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [{\n    \"json\": [{\n        \"Details\": {\n            \"TotalItemCount\": 1,\n            \"Items\": [{\n                \"ID\": 2485,\n                \"OrderNo\": \"2500\",\n                \"OrderDate\": \"2021-10-06T15:00:00+12:00\",\n                \"InvoiceCustomer\": {\n                    \"Code\": \"INTERNAL\"\n                },\n                \"DeliveryCustomer\": {\n                    \"Code\": \"INTERNAL\"\n                },\n                \"RequiredDateTime\": \"2021-10-10T15:00:00+12:00\",\n                \"OrderLines\": [{\n                        \"ProductCode\": \"PRODUCT001\",\n                        \"Description\": \"Test Product 1\",\n                        \"Quantity\": 45,\n                        \"Value\": {\n                            \"Net\": 45,\n                            \"Tax\": 6.75,\n                            \"Gross\": 51.75,\n                            \"TaxDetails\": {\n                                \"Code\": \"01\",\n                                \"Rate\": 15\n                            }\n                        }\n                    },\n                    {\n                        \"ProductCode\": \"PRODUCT002\",\n                        \"Description\": \"Test Product 2\",\n                        \"Quantity\": 45,\n                        \"Value\": {\n                            \"Net\": 45,\n                            \"Tax\": 6.75,\n                            \"Gross\": 51.75,\n                            \"TaxDetails\": {\n                                \"Code\": \"01\",\n                                \"Rate\": 15\n                            }\n                        }\n                    }\n                ]\n            }]\n        }\n    }]\n}];\n"
      },
      "name": "Sample Order Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        530,
        670
      ]
    },
    {
      "parameters": {
        "functionCode": "const OrderLines = [];\nconst RequiredDate = $json[\"0\"][\"Details\"][\"Items\"][\"0\"][\"RequiredDateTime\"];\nfor (item of items[0].json[0][\"Details\"][\"Items\"][0][\"OrderLines\"]) {\n  OrderLines.push({json:{StockItemCode: item.ProductCode, Description: item.Description, Quantity: item.Quantity, Value: item.Value, RequiredDate}})\n}\n\n// You can write logs to the browser console\n\nreturn OrderLines;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        810,
        670
      ]
    }
  ],
  "connections": {
    "Sample Order Data": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The Rename Keys node was a good idea, but it won’t be useful in this example, since you are trying to rename keys nested in an array. I hope my example helps :slight_smile:

1 Like

Hi @harshil1712,

That was amazing!! Thank you so much for your assistance. Much appreciated.

1 Like

Did that solve your issue? If yes, can you please mark the solution? This will help others with a similar question find the solution easily :slight_smile:

1 Like