How do I insert variable number of service on my automation

Describe the problem/error/question

Hello guys, I’m trying to prepare my automation to be smart when I have different numbers of items products on a sale and create a sale to post on other api.

Some times I’ll get 1 service, other time l’ll get 2 services and some I can get until 10 services to send to Pipedrive API.

Example with 1 service:

Example with 4 services:

With one service is working well, but I need to prepare to send other services at the same automation

image

Any Ideia how can I prepare my automation to be smarter?

Below is the api documentation url to create a sale.

https://developers.contaazul.com/#!/Sale/create

Cheers

  • n8n version: 1.11.2
  • *Database: SQLite:
  • n8n EXECUTIONS_PROCESS setting default: own, main:
  • Running n8n via Docker

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@Rodrigo_Pereira1 , you need to add “Items List” node to aggregate your services. Then it will not matter how many of the items you have - one or more. Here’s an example.

3 Likes

This is a pretty good solution, The first thing I thought of was to use a code node to build out the json to send but this would be easier.

Thanks for your help.

I try to use the Item Lists but I got an error after that.

After the node Item Lists is just getting the Items Lists Parameters

I can’t get the previous node parameters to insert on my expression.

Please see the image below:

Any idea to how can I fix this?

@Rodrigo_Pereira1 , if I understood you right, the latest issue you report is not with Item Lists bit with the expressions you use. There are tons of suggestions how to fix it in this forum (try to find them). The most common solution is to use first() instead of item (if there is only one item). Here’s the docs describing the usage, Output of other nodes | n8n Docs.

Hello @ihortom. Sorry, I didn’t explain well.

I’ll try to explain again using images.

When I use just the nodes without item lists my parameters are getting, as the two images below:

The problem was when I insert the node item lists between ‘Estrutura_de_Dados_Servicos_PJ’ node and ‘CA_Adiciona_Venda’ node.

As you see in the image above, my parameters before items lists node stopped to work and just the item list node parameter is getting.

I’d like to get all the previous parameters to complete my http node and send a post request.

@Rodrigo_Pereira1 , without seeing the actual data in “Estrutura” node it hard for me to tell you what your expression should look like when you reference the nodes prior to the previous one. However, the link I provided how to reference the nodes still stands and it should work.

Perhaps you need to combine the data from “Estrutura” node and the List Items. Again, I do not know what your data looks like and what it should look like for your final POST request.

As “Estrutura” node has 2 items in the image, does it mean you need to make 2 POST calls in HTTP Request node or it needs to be 1 request with 2 items presented in the array? Perhaps you can show what the data in “Estrutura” node is (and maybe other nodes) and how it should be presented to be used in the HTTP Request node, then I could guide you with how to transform it.

Anyway, assuming you need to combine the data from “Estrutura” node and the List Items (so that HTTP Request does not need to reference “Estrutura” node), here’s an example how you could do it.

Ohh thanks for the idea.

I try to use the merge node and get all the parameters, but now I have a new problem.

the output is getting an error:

@Rodrigo_Pereira1 , you still seem to reference the previous nodes. The whole idea behind using Merge node is that you get rid of $('<NODE>') and use just incoming data in the final HTTP Request. That is they all would be $json.<peroperty>. This will resolve your latest problem but you might want to update your references as it make it easier to maintain.

As for the error “JSON parameter need to be an valid JSON”, it looks like “Using JSON” option dose not like complex JSONs. However, this could be overcome by mapping the values manually with “Using Fields Below” option.

In that case, the offending property, namely "services" will have just value {{ $json.services }}. Nested part of JSON a bit more complex. If you have something like this

  "services": [
    {. . .}
  ],
  "payment": {
    "installments": [
      {
        "number": 1,
        "value": "something"
      }
    ]
  }

Then the field will be payment.installments[0].number and it’s value just 1. Similarly the “value” property would have to be presented as payment.installments[0].number with value “something”.

This depicted in the node below

Alternatively, you can introduce Set node between Merge and HTTP Request. This Set node would have all these manually set fields and the HTTP Request would have only one value, {{ $json }} with “Using JSON” option:

1 Like

@Rodrigo_Pereira1 , actually I figured it out how to make “Using JSON” option work with your configuration. You need to make the whole JSON as an expression. That is,

{{
{
  "emission": "$('Estrutura_de_Dados_PJ').item.json.data_atual_iso",
  "status": "COMMITTED",
  "customer_id": "$('Estrutura_de_Dados_Servicos_PJ').item.json.cliente_id",
  "seller_id": "57a79173-3a56-46a8-99c5-66ee5296fe6c",
  "services": $json["services"],
  ],
  "payment": {
    "type": "$('Estrutura_de_Dados_PJ').item.json.payment.type",
    "method": "$('Estrutura_de_Dados_PJ').item.json.payment.method",
    "installments": [
      {
        "number": 1,
        "value": $('Estrutura_de_Dados_Servicos_PJ').item.json.valor_produto,
        "due_date": "$('Estrutura_de_Dados_PJ').item.json.data_atual_iso",
        "status": "PENDING",
        "note": "NOTE"
      }
    ]
  },
  "notes": "Venda vindo da Automação"
}
}}

If you intend to keep Merge I would still advise you to replace $('<NODE>') with $json.<peroperty>.

Thanks so much.

I use the set note and structure all data there.

Cheers.

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