My first function?

I there,
so far I was able to get everything I wanted, by combining nodes - yay!

For the first time I am stuck now. Seems like I have to learn some JS…

I use a http-request to get a list for open Issues from Redmine.

I want to update the channel of a chat-app, with a message, that looks like that:

There are 5 open Issues:
#4 - ‘Build thing’ - open since 2021-05-03 - link
#7 - ‘Build another thing’ - open since 2021-06-03 - link
#9 - ‘Make Part’ - open since 2021-08-03 - link

Can so. help me to get started with my first my first function for that? Is the function-node the best way to do it?

Thank you guys!

J

Hey there,

So I do something something similar with Zendesk to slack with open and unassigned tickets.

The best I found at the moment was to use the function node to group your items into the format you want (in the items loop) then just return the complete message that you want to send.

I just checked the output of the endpoint to list the issues and noticed the issues are wrapped in the property issues. So, the first function needs to unwrap the issues array, so the data is easy to work with - you can skip this step, but I would not recommend it. The second function creates the message (as @jon suggested) you need to send.

Check the example workflow below. Just copy it and paste it into a new workflow.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        40,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.issues.issue.map(issue => ({ json: issue }))\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        300
      ],
      "notesInFlow": true,
      "notes": "Unwrap data"
    },
    {
      "parameters": {
        "url": "https://mockup-pj5l0yxsjrbr.runkit.sh/",
        "options": {
          "splitIntoItems": false
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        340,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "\nlet message = '';\n\nconst lineBreaker = '\\n'\n\nfor (const [index, item] of items.entries()) {\n  message+= `#${index} - '${item.json.subject}' - open since  ${item.json.created_on}${lineBreaker}`\n}\n\nreturn [{ json: { message } }]\n\n\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1040,
        300
      ],
      "notesInFlow": true,
      "notes": "Create message"
    },
    {
      "parameters": {
        "value": "={{ $node[\"Function\"].json[\"created_on\"] }}",
        "dataPropertyName": "created_on",
        "toFormat": "MM-DD-YYYY",
        "options": {}
      },
      "name": "Date & Time",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        830,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thank you!!

That is a great start to dig deeper into JS!

Glad that it worked. Have fun.