Send custom data to mautic custom fields

Hi guys,
I am playing around with n8n and mautic and so far it is amazing! :slight_smile:

Now I am stuck here:

I created a workflow where I receive the data from a new woocommerce order.
Inside my woocommerce I have a dropdown at checkout with several values.
The webhook sends the value when the order gets created and n8n receives it.
In Mautic I created the same custom field with the matching values.

And now it comes:
How can I map the new custom field data from the woocommerce webhook to mautic?
I guess I need to do it with the json-parameters. But how?
How do I need to write it that the right data gets sent to mautic and it knows where to put which value?

Would be really nice if someone could guide me here or give me a hintā€¦
Cheers :slight_smile:

Hi @alexhammerschmied,

I checked code node on Github (n8n/Mautic.node.ts at master Ā· n8n-io/n8n Ā· GitHub) and took the example with contact ressource + create operation (start line 279).

If you want to add an other field (which is not in the current list of additional fields), you have to create your own json body which will replace other default fields (First Name,Last Name,Primary Company,Position,Title).

You can choose ā€œJSON Parametersā€ button and add your own expression in order to create the json inside the expression.
For example :

'
"mycustomfield" : "{{$json["myCustomField"]}}"
'

where $json[ā€œmyCustomFieldā€] is the field got in the current data node (variable selector).

Iā€™m not using woomerce & mautic but you can try this

3 Likes

Thatā€™s great. Thank you @non0!

Would it be possible to fetch the custom fields from a mautic instance and show them inside the ā€œadditional fieldsā€ dropdown? That would be an awesome feature!

1 Like

Hey @alexhammerschmied!

If the Mautic API supports that, I think it is possible. Can you please create a feature request for this?

Thanks :slightly_smiling_face:

1 Like

yes sure. I post it right now. I added it here:

1 Like

@non0
I am doing something wrong i guess.
this is the error i get:
ERROR: Cannot read property ā€˜lengthā€™ of undefined

TypeError: Cannot read property 'length' of undefined
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:451:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:395:62

@alexhammerschmied Iā€™ve created a small workflow that might help.

1 Like

yes thatā€™s what i created as well. Thank you for that!

Now I know where the error comes from:
I am using an ā€œifā€ node after looking for an existing contact.
My problem is now, that if the contact doesnā€™t exist, the if has no data and that creates the error when i try to create a new contact with the following mautic nodeā€¦

So my test if the contact exists and if not then should get created doesnā€™t work.

Iā€™ll see if I can figure this out.

Itā€™s great that you figured out that the IF node is giving the error!

If you need help, feel free to share your workflow (copy the nodes and paste them here). It will help me recreate your error and help you find a solution.

1 Like

@harshil1712 thank you for helping me out! :slight_smile:

This is what I built so far:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -770,
        410
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "jsonParameters": true,
        "bodyJson": "={\n\"email\" : \"{{$node[\"Set Webhook Request\"].json[\"email\"]}}\",\n\"firstname\" : \"{{$node[\"Set Webhook Request\"].json[\"firstname\"]}}\",\n\"lastname\" : \"{{$node[\"Set Webhook Request\"].json[\"lastname\"]}}\",\n\"address1\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_1\"]}}\",\n\"address2\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_2\"]}}\",\n\"city\" : \"{{$node[\"Set Webhook Request\"].json[\"city\"]}}\",\n\"zipcode\" : \"{{$node[\"Set Webhook Request\"].json[\"zip_code\"]}}\",\n\"phone\" : \"{{$node[\"Set Webhook Request\"].json[\"phone\"]}}\",\n\"newsletter\" : \"{{$node[\"Set Webhook Request\"].json[\"newsletter\"]}}\",\n\"kaffeevollautomat_modell\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell\"]}}\",\n\"kaffeevollautomat_modell1\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell_custom\"]}}\",\n\"ordered_products\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\",\n\"order_status\" : \"{{$node[\"Set Webhook Request\"].json[\"status\"]}}\",\n\"last_order_date\" : \"{{$node[\"Set Webhook Request\"].json[\"last_order_date\"]}}\",\n\"tags\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\"\n}",
        "additionalFields": {},
        "options": {
          "rawData": false
        }
      },
      "name": "Mautic",
      "type": "n8n-nodes-base.mautic",
      "position": [
        30,
        560
      ],
      "typeVersion": 1,
      "alwaysOutputData": false,
      "credentials": {
        "mauticOAuth2Api": " mautic"
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "getAll",
        "limit": 1,
        "options": {
          "search": "={{$node[\"Set Webhook Request\"].json[\"email\"]}}"
        }
      },
      "name": "Find User",
      "type": "n8n-nodes-base.mautic",
      "position": [
        -400,
        410
      ],
      "notesInFlow": false,
      "typeVersion": 1,
      "alwaysOutputData": false,
      "credentials": {
        "mauticOAuth2Api": " mautic"
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"Set Webhook Request\"].json[\"email\"]}}",
              "operation": "regex",
              "value2": "={{$node[\"Find User\"].json[\"fields\"][\"all\"][\"email\"]}}"
            }
          ]
        }
      },
      "name": "IF NOT userFound1",
      "type": "n8n-nodes-base.if",
      "position": [
        -230,
        410
      ],
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "firstname",
              "value": "Alex"
            },
            {
              "name": "lastname",
              "value": "Test2021-1"
            },
            {
              "name": "email",
              "value": "[email protected]"
            },
            {
              "name": "phone",
              "value": "657432895678"
            },
            {
              "name": "address_line_1",
              "value": "TeststraƟe 28"
            },
            {
              "name": "address_line_2"
            },
            {
              "name": "city",
              "value": "Testort"
            },
            {
              "name": "zip_code",
              "value": "1234"
            },
            {
              "name": "product id",
              "value": "3622"
            },
            {
              "name": "status",
              "value": "3622 processing"
            },
            {
              "name": "last_order_date",
              "value": "2021-01-04T12:03:24"
            },
            {
              "name": "last_order_fulfilled"
            },
            {
              "name": "attribution",
              "value": "25"
            },
            {
              "name": "newsletter",
              "value": "1"
            },
            {
              "name": "kvamodell",
              "value": "DeLonghi PrimaDonna Soul"
            },
            {
              "name": "kvamodell_custom"
            }
          ],
          "number": [],
          "boolean": []
        },
        "options": {}
      },
      "name": "Set Webhook Request",
      "type": "n8n-nodes-base.set",
      "position": [
        -580,
        410
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "update",
        "contactId": "={{$node[\"Find User\"].json[\"id\"]}}",
        "jsonParameters": true,
        "updateFields": {
          "bodyJson": "={\n\"firstname\" : \"{{$node[\"Set Webhook Request\"].json[\"firstname\"]}}\",\n\"lastname\" : \"{{$node[\"Set Webhook Request\"].json[\"lastname\"]}}\",\n\"address1\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_1\"]}}\",\n\"address2\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_2\"]}}\",\n\"city\" : \"{{$node[\"Set Webhook Request\"].json[\"city\"]}}\",\n\"zipcode\" : \"{{$node[\"Set Webhook Request\"].json[\"zip_code\"]}}\",\n\"phone\" : \"{{$node[\"Set Webhook Request\"].json[\"phone\"]}}\",\n\"newsletter\" : \"{{$node[\"Set Webhook Request\"].json[\"newsletter\"]}}\",\n\"kaffeevollautomat_modell\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell\"]}}\",\n\"kaffeevollautomat_modell1\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell_custom\"]}}\",\n\"ordered_products\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\",\n\"order_status\" : \"{{$node[\"Set Webhook Request\"].json[\"status\"]}}\",\n\"last_order_date\" : \"{{$node[\"Set Webhook Request\"].json[\"last_order_date\"]}}\",\n\"tags\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\"\n}"
        },
        "options": {
          "rawData": false
        }
      },
      "name": "Mautic1",
      "type": "n8n-nodes-base.mautic",
      "position": [
        30,
        280
      ],
      "typeVersion": 1,
      "alwaysOutputData": false,
      "credentials": {
        "mauticOAuth2Api": " mautic"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Webhook Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find User": {
      "main": [
        [
          {
            "node": "IF NOT userFound1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF NOT userFound1": {
      "main": [
        [
          {
            "node": "Mautic1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mautic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Webhook Request": {
      "main": [
        [
          {
            "node": "Find User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I did it and found the mistake.
There was no data passed to the IF Function if the user was not existing yet.

This is the final version. Feel free to use it:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -770,
        410
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "getAll",
        "limit": 1,
        "options": {
          "search": "={{$node[\"Set Webhook Request\"].json[\"email\"]}}"
        }
      },
      "name": "Find User",
      "type": "n8n-nodes-base.mautic",
      "position": [
        -430,
        260
      ],
      "notesInFlow": false,
      "typeVersion": 1,
      "alwaysOutputData": true,
      "credentials": {
        "mauticOAuth2Api": "Mautic"
      }
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "firstname",
              "value": "Alex"
            },
            {
              "name": "lastname",
              "value": "test20212"
            },
            {
              "name": "email",
              "value": "[email protected]"
            },
            {
              "name": "phone",
              "value": "657432895678"
            },
            {
              "name": "address_line_1",
              "value": "TeststraƟe 28"
            },
            {
              "name": "address_line_2"
            },
            {
              "name": "city",
              "value": "Testort"
            },
            {
              "name": "zip_code",
              "value": "1234"
            },
            {
              "name": "product id",
              "value": "3622"
            },
            {
              "name": "status",
              "value": "3622 processing"
            },
            {
              "name": "last_order_date",
              "value": "2021-01-04T12:03:24"
            },
            {
              "name": "last_order_fulfilled"
            },
            {
              "name": "attribution",
              "value": "25"
            },
            {
              "name": "newsletter",
              "value": "1"
            },
            {
              "name": "kvamodell",
              "value": "DeLonghi PrimaDonna Soul"
            },
            {
              "name": "kvamodell_custom"
            }
          ],
          "number": [],
          "boolean": []
        },
        "options": {}
      },
      "name": "Set Webhook Request",
      "type": "n8n-nodes-base.set",
      "position": [
        -580,
        410
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "userFound",
              "value": "={{$node[\"If not found return -1\"].json[\"id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -110,
        250
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.id = items[0].json.id || -1\nreturn items;"
      },
      "name": "If not found return -1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -260,
        250
      ]
    },
    {
      "parameters": {
        "mode": "mergeByIndex",
        "join": "inner"
      },
      "name": "main customer data",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        70,
        390
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"main customer data\"].json[\"userFound\"]}}",
              "operation": "regex",
              "value2": "-1"
            }
          ]
        }
      },
      "name": "IF NOT userFound",
      "type": "n8n-nodes-base.if",
      "position": [
        280,
        390
      ],
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "jsonParameters": true,
        "bodyJson": "={\n\"email\" : \"{{$node[\"Set Webhook Request\"].json[\"email\"]}}\",\n\"firstname\" : \"{{$node[\"Set Webhook Request\"].json[\"firstname\"]}}\",\n\"lastname\" : \"{{$node[\"Set Webhook Request\"].json[\"lastname\"]}}\",\n\"address1\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_1\"]}}\",\n\"address2\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_2\"]}}\",\n\"city\" : \"{{$node[\"Set Webhook Request\"].json[\"city\"]}}\",\n\"zipcode\" : \"{{$node[\"Set Webhook Request\"].json[\"zip_code\"]}}\",\n\"phone\" : \"{{$node[\"Set Webhook Request\"].json[\"phone\"]}}\",\n\"newsletter\" : \"{{$node[\"Set Webhook Request\"].json[\"newsletter\"]}}\",\n\"kaffeevollautomat_modell\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell\"]}}\",\n\"kaffeevollautomat_modell1\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell_custom\"]}}\",\n\"ordered_products\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\",\n\"order_status\" : \"{{$node[\"Set Webhook Request\"].json[\"status\"]}}\",\n\"last_order_date\" : \"{{$node[\"Set Webhook Request\"].json[\"last_order_date\"]}}\",\n\"tags\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\"\n}",
        "additionalFields": {},
        "options": {
          "rawData": false
        }
      },
      "name": "Create New Contact",
      "type": "n8n-nodes-base.mautic",
      "position": [
        510,
        280
      ],
      "typeVersion": 1,
      "alwaysOutputData": false,
      "credentials": {
        "mauticOAuth2Api": "Mautic"
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "update",
        "contactId": "={{$node[\"Find User\"].json[\"id\"]}}",
        "jsonParameters": true,
        "updateFields": {
          "bodyJson": "={\n\"firstname\" : \"{{$node[\"Set Webhook Request\"].json[\"firstname\"]}}\",\n\"lastname\" : \"{{$node[\"Set Webhook Request\"].json[\"lastname\"]}}\",\n\"address1\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_1\"]}}\",\n\"address2\" : \"{{$node[\"Set Webhook Request\"].json[\"address_line_2\"]}}\",\n\"city\" : \"{{$node[\"Set Webhook Request\"].json[\"city\"]}}\",\n\"zipcode\" : \"{{$node[\"Set Webhook Request\"].json[\"zip_code\"]}}\",\n\"phone\" : \"{{$node[\"Set Webhook Request\"].json[\"phone\"]}}\",\n\"newsletter\" : \"{{$node[\"Set Webhook Request\"].json[\"newsletter\"]}}\",\n\"kaffeevollautomat_modell\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell\"]}}\",\n\"kaffeevollautomat_modell1\" : \"{{$node[\"Set Webhook Request\"].json[\"kvamodell_custom\"]}}\",\n\"ordered_products\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\",\n\"order_status\" : \"{{$node[\"Set Webhook Request\"].json[\"status\"]}}\",\n\"last_order_date\" : \"{{$node[\"Set Webhook Request\"].json[\"last_order_date\"]}}\",\n\"tags\" : \"{{$node[\"Set Webhook Request\"].json[\"product id\"]}}\"\n}"
        },
        "options": {
          "rawData": false
        }
      },
      "name": "Update existing contact",
      "type": "n8n-nodes-base.mautic",
      "position": [
        510,
        510
      ],
      "typeVersion": 1,
      "alwaysOutputData": false,
      "credentials": {
        "mauticOAuth2Api": "Mautic"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Webhook Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find User": {
      "main": [
        [
          {
            "node": "If not found return -1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Webhook Request": {
      "main": [
        [
          {
            "node": "main customer data",
            "type": "main",
            "index": 1
          },
          {
            "node": "Find User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "main customer data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If not found return -1": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "main customer data": {
      "main": [
        [
          {
            "node": "IF NOT userFound",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF NOT userFound": {
      "main": [
        [
          {
            "node": "Create New Contact",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update existing contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

I am glad you found the solution! Have fun!