Help with gpt generated code

Hello, so I’m a total newbie to automation and I rely heavily on Gemini or GPT for coding guidance. Here’s my goal: I’m getting opportunities from pipelines via an HTTPS request (previous node) and I need to count them by status for a weekly tally. Can someone break down this GPT-generated code for me or offer some tips to simplify the process? Thanks a ton!

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

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

Hey @stonewall you’re on the right track, but the loop issue is causing it to only process the first record. Try iterating directly over the opportunities array like this

const opportunities = $json.opportunities;const statusCounts = {};
for (const opp of opportunities) {const status = opp.status;statusCounts[status] = (statusCounts[status] || 0) + 1;}

return [{ json: statusCounts }];


This way, each opportunity is counted correctly, and you’ll get an object with the totals for each status.

Hope this helps!

1 Like

Oh I see, yeaah I thought the input.first part is important, I guess not :sweat_smile:. Thank you so much I’ll try it! But is there another thing I need to map or is the code above the only thing needed? But Thank you very much for this!

3 Likes

No need to map anything else, the code above should do the trick. Just plug it in and test it out. If you need to handle any specific edge cases or add more functionality, feel free to ask. Otherwise, you’re good to go! :+1:

Kindly mark as the solution

2 Likes

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