Best way to increase number in URL?

Describe the problem/error/question

Hey everyone, I have a question. My goal is to send a comment URL that looks like this: “{$json.body.payload.url}#note-[comment number]”. The result should look like this: “http://192.168.32.223/issues/2492#note-1”. I want the number to keep incrementing every time the function is called.

Here is my code:

// Initialize the counter variable
let counter = $('Edit Fields').item.json.number || 0;

// Define the function to process the input
function processInput(input) {
    // Increment the counter for the next iteration
    counter++;

    // Extract notes from the input JSON
    const regex = /@(\w+)/g;
    const match = input.match(regex);

    // Construct the new URL with the incremented counter
    const newURL = `${$json.body.payload.url}#note-${counter}`;

    // Remove special characters from the input value
    const cleanedInput = input.replace(/[\u007F-\uFFFF]/g, '');

    // Return a single object representing the output item with formatted line breaks
    return {
        input: cleanedInput.replace(/\r\n/g, '\\r\\n'),
        match: match ? match.map(m => m.replace(/^"(.*)"$/, '$1')) : [],
        newURL: newURL
    };
}

// Call the function with the input
const result = processInput($json.body.payload.journal?.notes || '');

// Return the result
return result;

This code initializes a counter variable, increments it each time the function is called, and constructs a new URL with the incremented counter. It also extracts notes from the input JSON, removes special characters from the input value, and returns a single object representing the output item with formatted line breaks.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

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

Hello @Winniepoom

You can simply use the $itemIndex variable

{{$json.body.payload.url}}#note-{{$itemIndex+1}}

@barn4k Hey, thank you for your answer. I tried putting the $itemIndex variable in the Slack node, but it seems to always output URL number 1 and it not increasing , whether I run the whole workflow or just the Slack node. Did I miss something?"

{
	"blocks": [
		{
			"type": "header",
			"text": {
				"type": "plain_text",
				"text": " 📝 Redmine Note 📝"
			}
		},
		{
			"type": "divider"
		},
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "*Note :*  \n * Subject:* {{ $('Webhook1').first().json.body.payload.issue.subject }}\n*URL :* {{ $('Webhook1').first().json.body.payload.url }}#note-{{$itemIndex+1}} \n"
			}
		},
		{
			"type": "divider"
		}
	]
}

By funtion you mean workflow?
If so, you need to save the variable somewhere external like a spreadsheet or DB.
Or you search in slack for the last related message and go from there, but it seems like more overhead.

1 Like

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