Search function for specific custom fields in Trello cards

Hello,

When getting the data from a Trello custom field the name changes [“customFieldItems”][0-10] based on the other custom fields with data or no data within the card.

This has been really hard for me to identify specific fields because when I change, enter or erase the data on any field, the item number changes on the output I receive on n8n.

How can I get each custom field value regardless if the field number changes?


These are the field that change.

This is my workflow

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -10,
        110
      ],
      "disabled": true
    },
    {
      "parameters": {
        "operation": "getAll",
        "limit": 1,
        "options": {
          "search": "={{$node[\"Set\"].json[\"Email\"]}}"
        }
      },
      "name": "Buscar el contacto",
      "type": "n8n-nodes-base.mautic",
      "typeVersion": 1,
      "position": [
        1190,
        -90
      ],
      "credentials": {
        "mauticApi": "Mautic Login n8n"
      }
    },
    {
      "parameters": {
        "resource": "campaignContact",
        "contactId": "={{$node[\"Buscar el contacto\"].json[\"fields\"][\"all\"][\"id\"]}}",
        "campaignId": "=79"
      },
      "name": "Agregar a retoques",
      "type": "n8n-nodes-base.mautic",
      "typeVersion": 1,
      "position": [
        1350,
        -90
      ],
      "credentials": {
        "mauticApi": "Mautic Login n8n"
      }
    },
    {
      "parameters": {
        "id": "5bec206ceec21d055b590279"
      },
      "name": "Mover a retoques",
      "type": "n8n-nodes-base.trelloTrigger",
      "typeVersion": 1,
      "position": [
        150,
        110
      ],
      "webhookId": "45ca3332-1718-4fbe-9a3e-078d6f772f8a",
      "credentials": {
        "trelloApi": "Trello GDP"
      }
    },
    {
      "parameters": {
        "operation": "get",
        "id": "={{$node[\"Mover a retoques\"].json[\"action\"][\"data\"][\"card\"][\"id\"]}}",
        "additionalFields": {
          "fields": "all",
          "customFieldItems": true
        }
      },
      "name": "Buscar tarjeta",
      "type": "n8n-nodes-base.trello",
      "typeVersion": 1,
      "position": [
        590,
        10
      ],
      "credentials": {
        "trelloApi": "Trello GDP"
      }
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [],
          "string": [
            {
              "value1": "={{$node[\"Set\"].json[\"Lista Anterior\"]}}",
              "operation": "notEqual",
              "value2": "={{$node[\"Set\"].json[\"Lista Después\"]}}"
            },
            {
              "value1": "={{$node[\"Set\"].json[\"Retoque\"]}}",
              "value2": "true"
            },
            {
              "value1": "={{$node[\"Set\"].json[\"Lista Anterior\"]}}",
              "operation": "notEqual",
              "value2": "Retoque"
            }
          ]
        }
      },
      "name": "Solo a Retoques",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1000,
        10
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Retoque",
              "value": "={{ $json[\"customFieldItems\"][10] ? $json[\"customFieldItems\"][10][\"idCustomField\"] : 'not found' }}\n\n{{Object.values($node[\"Buscar tarjeta\"].json[\"customFieldItems\"][10][\"value\"]).join(', ')}}\n\n{{$json[\"customFieldItems\"][10][\"idCustomField\"]}}\n{{$json[\"customFieldItems\"][10][\"id\"]}}\n{{$json[\"customFieldItems\"][10][\"modelType\"]}}"
            },
            {
              "name": "Lista Anterior",
              "value": "={{$node[\"Mover a retoques\"].json[\"action\"][\"data\"][\"listBefore\"][\"name\"]}}"
            },
            {
              "name": "Lista Después",
              "value": "={{$node[\"Mover a retoques\"].json[\"action\"][\"data\"][\"listAfter\"][\"name\"]}}"
            },
            {
              "name": "Email",
              "value": "={{$node[\"Buscar tarjeta\"].json[\"customFieldItems\"][10][\"value\"][\"text\"]}}"
            },
            {
              "name": "Album",
              "value": "={{$node[\"Buscar tarjeta\"].json[\"customFieldItems\"][5][\"value\"][\"text\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        790,
        10
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{Object.values($node[\"Mover a retoques\"].json[\"action\"][\"data\"][\"customFieldItem\"][\"value\"]).join(', ')}}",
              "value2": "true"
            },
            {
              "value1": "={{$node[\"Mover a retoques\"].json[\"action\"][\"data\"][\"customFieldItem\"][\"idCustomField\"]}}",
              "value2": "5c818e0bf288db82b847c59e"
            }
          ]
        }
      },
      "name": "Retoque Check",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        330,
        110
      ]
    }
  ],
  "connections": {
    "Buscar el contacto": {
      "main": [
        [
          {
            "node": "Agregar a retoques",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mover a retoques": {
      "main": [
        [
          {
            "node": "Retoque Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar tarjeta": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Solo a Retoques": {
      "main": [
        [
          {
            "node": "Buscar el contacto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Solo a Retoques",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retoque Check": {
      "main": [
        [
          {
            "node": "Buscar tarjeta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
}```

Hi @zerog, hope you are well?

I tried creating a few custom fields on my end and it appears this can easily be achieved through the Function Item node. Here’s what I did:

  1. First, I opened the JSON version of my Trello board by simply appending .json to its URL
  2. In there you’ll find an array under the key customFields holding all your custom fields and looking like this:
"customFields": [{
    "id": "61767b5b71d35f8b909bcdfe",
    "idModel": "61767a546692fd5aa6400f31",
    "modelType": "board",
    "fieldGroup": "be729f02aed55812ba7e5ec9c7ce36bab0ea96807780957a998f4d7fb1560f7b",
    "display": {
        "cardFront": true
    },
    "name": "My Text",
    "pos": 81920,
    "type": "text",
    "isSuggestedField": false
}, ... ]
  1. Copy the id value from the required custom field
  2. Now, in n8n, you can add a Function Item node after your Trello node getting the respective card. You can then write the content of your custom field in a named field of your n8n item by running code like below in the Function Item node (replace my id with the correct id from in step 2). This uses JavaScript’s find() method to find the custom field with the given ID regardless of the array order.
item.myText = item.customFieldItems.find(field => field.idCustomField == '61767b5b71d35f8b909bcdfe').value;

return item;
  1. Now your items will contain the value of the respective custom field in their own myText field, regardless of the order of custom items.

My example workflow for this looked like so:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "get",
        "id": "eLlVvpr2",
        "additionalFields": {
          "fields": "all",
          "customFieldItems": true
        }
      },
      "name": "Trello",
      "type": "n8n-nodes-base.trello",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "trelloApi": {
          "id": "27",
          "name": "[email protected]"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "item.myText = item.customFieldItems.find(field => field.idCustomField == '61767b5b71d35f8b909bcdfe').value;\n\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Trello",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trello": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope this makes sense and helps!

Thank you for replying @MutedJam

I followed every step you explained and I’m receiving this error:

ERROR: item is not defined

Details

Stack

ReferenceError: item is not defined
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:1:98
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:4:2)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1233:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:81:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:517:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:447:62
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Also, I want to point out that my json structure is different than the one you have in your example. I don’t know if that is an issue.

[
{
"id": "61420f16d3dc4e33344f2482",
"idList": "5bec208e327e1704762fdc28",
"customFieldItems": [

{
"id": "6169723bc92a1b5aea102af2",
"value": {
"text": "Pro 8x10"
},
"idCustomField": "5c7aeb562f24010c8184d690",
"idModel": "61420f16d3dc4e33344f2482",
"modelType": "card"
},

]
}
]

ERROR: item is not defined

Are you possibly using the Function node instead of the Function Item node @zerog? Only the latter would loop through individual items.

The JSON you have shared seems to belong to an individual card. Could you look for the top level customFields key?

That was the problem. I was using the Function node and and it should be the Function Item node as you state.

Everything is working now with your first solution.

Thank you so much for the help.

1 Like

Awesome, I am glad to hear that! Thanks so much for confirming :smiley:

1 Like