# Ticket workflow custom code to create associated note

**URL:** <https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295>\
**Category:** APIs & Integrations\
**Tags:** workflows, developer-tools\
**Created:** [July 30, 2024, 3:24pm UTC](https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295 "2024-07-30T15:24:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatBehr](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/matbehr/32/27089_2.png) [@MatBehr](https://community.hubspot.com/u/MatBehr)\
**Post date:** [July 30, 2024, 3:24pm UTC](https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295/1 "2024-07-30T15:24:19Z")

</div>

Hi All,

Just to preface i am a complete beginner with code. What i am trying to accomplish is a note being created on an enrolled ticket that contains special instruction on how to handle specific customers. the code i have cobbled together is as follows, (using a private app)

const hubspot = require(‘@hubspot/api-client’);  
exports.main = async () =\> {  
const token = process.env.OPS\_TOKEN

const hubspotClient = new hubspot.Client({“accessToken”:token});

const properties = {  
“hs\_timestamp”: Date.now(), // Current time/date,  
“hs\_note\_body”: “testing note creation” // Body of the note,  
};  
const SimplePublicObjectInputForCreate = { associations: [{“types”:[{“associationCategory”:“HUBSPOT\_DEFINED”,“associationTypeId”:228}],“to”:{“id”:“hs\_object\_id”}}], properties };

try {  
const apiResponse = await hubspotClient.crm.objects.notes.basicApi.create(SimplePublicObjectInputForCreate);  
console.log(JSON.stringify(apiResponse, null, 2));  
} catch (e) {  
e.message === ‘HTTP request failed’  
? console.error(JSON.stringify(e.response, null, 2))  
: console.error(e)  
}}

```auto
WARNING: The logs for this function have exceeded the 4KB limit.
...
,"context":{"id":["hs_object_id"]},"category":"OBJECT_NOT_FOUND"}
Headers: {"access-control-allow-credentials":"false","cf-cache-status":"DYNAMIC","cf-ray":"8ab64e11bbadc946-IAD","connection":"keep-alive","content-encoding":"gzip","content-type":"application/json;charset=utf-8","date":"Tue, 30 Jul 2024 15:15:35 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=DlL2tufyf5hwqkNFpqnuX1ooIs%2B4mzKw%2BXYeaASlUElsAdZJK4IMEFczXc8W4qSgoTq2y1VLlyU09bIXqh2lWQQyukyHRbZ2J6LFz1%2Fto32IMZvDDE3zh0sU6Mq%2BHTkr\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","strict-transport-security":"max-age=31536000; includeSubDomains; preload","transfer-encoding":"chunked","vary":"origin, Accept-Encoding","x-content-type-options":"nosniff","x-envoy-upstream-service-time":"71","x-evy-trace-listener":"listener_https","x-evy-trace-route-configuration":"listener_https/all","x-evy-trace-route-service-name":"envoyset-translator","x-evy-trace-served-by-pod":"iad02/hubapi-td/envoy-proxy-7dd59b876-tndmq","x-evy-trace-virtual-host":"all","x-hubspot-correlation-id":"9eb78ec2-5940-48fb-b9bf-57785fe8c203","x-hubspot-notfound":"true","x-hubspot-ratelimit-daily":"500000","x-hubspot-ratelimit-daily-remaining":"499994","x-hubspot-ratelimit-interval-milliseconds":"10000","x-hubspot-ratelimit-max":"150","x-hubspot-ratelimit-remaining":"149","x-hubspot-ratelimit-secondly":"15","x-hubspot-ratelimit-secondly-remaining":"14","x-request-id":"9eb78ec2-5940-48fb-b9bf-57785fe8c203"}
 at BasicApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/notes/apis/BasicApi.js:209:23)
 at Generator.next (<anonymous>)
 at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/notes/apis/BasicApi.js:5:58)
 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
 code: 404,
 body: {
 status: 'error',
 message: 'Object not found. objectId are usually numeric.',
 correlationId: '9eb78ec2-5940-48fb-b9bf-57785fe8c203',
 context: { id: [Array] },
 category: 'OBJECT_NOT_FOUND'
 },
 headers: {
 'access-control-allow-credentials': 'false',
 'cf-cache-status': 'DYNAMIC',
 'cf-ray': '8ab64e11bbadc946-IAD',
 connection: 'keep-alive',
 'content-encoding': 'gzip',
 'content-type': 'application/json;charset=utf-8',
 date: 'Tue, 30 Jul 2024 15:15:35 GMT',
 nel: '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}',
 'report-to': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=DlL2tufyf5hwqkNFpqnuX1ooIs%2B4mzKw%2BXYeaASlUElsAdZJK4IMEFczXc8W4qSgoTq2y1VLlyU09bIXqh2lWQQyukyHRbZ2J6LFz1%2Fto32IMZvDDE3zh0sU6Mq%2BHTkr"}],"group":"cf-nel","max_age":604800}',
 server: 'cloudflare',
 'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
 'transfer-encoding': 'chunked',
 vary: 'origin, Accept-Encoding',
 'x-content-type-options': 'nosniff',
 'x-envoy-upstream-service-time': '71',
 'x-evy-trace-listener': 'listener_https',
 'x-evy-trace-route-configuration': 'listener_https/all',
 'x-evy-trace-route-service-name': 'envoyset-translator',
 'x-evy-trace-served-by-pod': 'iad02/hubapi-td/envoy-proxy-7dd59b876-tndmq',
 'x-evy-trace-virtual-host': 'all',
 'x-hubspot-correlation-id': '9eb78ec2-5940-48fb-b9bf-57785fe8c203',
 'x-hubspot-notfound': 'true',
 'x-hubspot-ratelimit-daily': '500000',
 'x-hubspot-ratelimit-daily-remaining': '499994',
 'x-hubspot-ratelimit-interval-milliseconds': '10000',
 'x-hubspot-ratelimit-max': '150',
 'x-hubspot-ratelimit-remaining': '149',
 'x-hubspot-ratelimit-secondly': '15',
 'x-hubspot-ratelimit-secondly-remaining': '14',
 'x-request-id': '9eb78ec2-5940-48fb-b9bf-57785fe8c203'
 }
}

Memory: 87/128 MB
Runtime: 522.71 ms

```

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [July 31, 2024, 3:01pm UTC](https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295/2 "2024-07-31T15:01:21Z")

</div>

Hey, @MatBehr 👋 That error looks like it’s coming from the workflow not recognizing the objectId “,“context”:{“id”:[“hs\_object\_id”]},“category”:“OBJECT\_NOT\_FOUND”}” I’d start by testing with a hard-coded value and then once successful, looking at how you are declaring the variable for `id`.

If you’ve already done this, please follow up here and we’ll take another look.

Best,

Jaycee

---

<div class="post-metadata">

**Author:** ![MatBehr](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/matbehr/32/27089_2.png) [@MatBehr](https://community.hubspot.com/u/MatBehr)\
**Post date:** [August 1, 2024, 8:02pm UTC](https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295/3 "2024-08-01T20:02:00Z")

</div>

Hi Jaycee,

hard-coding the value works and the note is successfully associated. so the issue is with the variable.

Thanks

MAt

---

<div class="post-metadata">

**Author:** ![MatBehr](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/matbehr/32/27089_2.png) [@MatBehr](https://community.hubspot.com/u/MatBehr)\
**Post date:** [August 1, 2024, 8:30pm UTC](https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295/4 "2024-08-01T20:30:15Z")

</div>

![MatBehr_0-1722543958886.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/8/84604f5e17c1dca0f3e0550299b1e2b1b28e5fb2.png)

so when i hover my input field for getting my ticket id for association it says its not used,

const hubspot = require(‘@hubspot/api-client’);  
exports.main = async (event) =\> {  
const token = process.env.OPS\_TOKEN

const hubspotClient = new hubspot.Client({“accessToken”:token});

const hs\_ticket\_id = event.inputFields[‘hs\_ticket\_id’];

const properties = {  
“hs\_timestamp”: Date.now(), // Current time/date,  
“hs\_note\_body”: “testing note creation” // Body of the note,  
};  
const SimplePublicObjectInputForCreate = { associations: [{“types”:[{“associationCategory”:“HUBSPOT\_DEFINED”,“associationTypeId”:228}],“to”:{“id”:"

hs\_ticket\_id

"}}], properties };

try {  
const apiResponse = await hubspotClient.crm.objects.notes.basicApi.create(SimplePublicObjectInputForCreate);  
console.log(JSON.stringify(apiResponse, null, 2));  
} catch (e) {  
e.message === ‘HTTP request failed’  
? console.error(JSON.stringify(e.response, null, 2))  
: console.error(e)  
}}

i assumed as i had defined the field above it could be used as a ref in the association.

Thanks

Mat

---

<div class="post-metadata">

**Author:** ![MatBehr](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/matbehr/32/27089_2.png) [@MatBehr](https://community.hubspot.com/u/MatBehr)\
**Post date:** [August 1, 2024, 9:07pm UTC](https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295/5 "2024-08-01T21:07:13Z")

</div>

I have this working now with some trial and error.

const hubspot = require(‘@hubspot/api-client’);

exports.main = async (event) =\> {  
const token = process.env.OPS\_TOKEN; // Calls the Private app  
const ticketId = event.inputFields.ticketId; // Pulls the Ticket id into the code

if (!token) {  
console.error(‘OPS\_TOKEN is not set’);  
return;  
}

if (!ticketId) {  
console.error(‘TICKET\_ID is not set’);  
return;  
}

console.log(‘Using ticket ID:’, ticketId);

const hubspotClient = new hubspot.Client({ “accessToken”: token });

// Fetch the ticket information  
let ticket;  
try {  
ticket = await hubspotClient.crm.tickets.basicApi.getById(ticketId);  
console.log(‘Ticket retrieved:’, JSON.stringify(ticket, null, 2));  
} catch (e) {  
console.error(‘Error fetching ticket:’, e);  
return;  
}

// Step 2: Use the ticket ID to create a note  
const properties = {  
“hs\_timestamp”: Date.now(), // Current time/date  
“hs\_note\_body”: “testing note creation” // Edit this section with the required message  
};

const SimplePublicObjectInputForCreate = {  
associations: [{  
“types”: [{ “associationCategory”: “HUBSPOT\_DEFINED”, “associationTypeId”: 228 }], // Use correct association type ID for tickets  
“to”: { “id”: ticketId }  
}],  
properties  
};

try {  
const apiResponse = await hubspotClient.crm.objects.notes.basicApi.create(SimplePublicObjectInputForCreate);  
console.log(JSON.stringify(apiResponse, null, 2));  
} catch (e) {  
e.message === ‘HTTP request failed’  
? console.error(JSON.stringify(e.response, null, 2))  
: console.error(e);  
}  
};

---

<div class="post-metadata">

**Author:** ![karstenkoehler](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/karstenkoehler/32/68554_2.png) [@karstenkoehler](https://community.hubspot.com/u/karstenkoehler)\
**Post date:** [November 8, 2024, 5:25am UTC](https://community.hubspot.com/t/ticket-workflow-custom-code-to-create-associated-note/113295/6 "2024-11-08T05:25:16Z")

</div>

Hi @marcodirk,

Good news, this is now possible!

_Workflows_  
_ **Create note workflow action:** _ [Create custom workflow actions with the action builder (BETA) - HubSpot docs](https://developers.hubspot.com/docs/api-reference/2026-03/automation/workflow-actions/custom-action-builder)  
_November 7, 2024_

_ **What is it?** _

_You can now automatically create notes using the new “Create note” workflow action._

_ **Why does it matter?** _

_Notes are a versatile tool for adding important context to records, highlighting key information, or summarizing changes for your team. With the new “Create note” workflow action, you can now automate the addition of notes to records as part of any process. For instance, when handing a customer off to the onboarding team, you can use a workflow to automatically add a note summarizing the information gathered during the sales process. Any data from the workflow can be included in the note body, including enrolled record properties, associated record properties, and workflow run time data like action outputs._

_ **How does it work?** _

_To use the action, create a new workflow and set up your trigger._

_Search workflow actions or view the CRM action category to find the “Create note” action_  
_Select the action and draft the note body_  
_Add personalization tokens from the enrolled record, associated record, or workflow action outputs to the note body as desired_

_The workflow automatically associates the note with the enrolled record._

_ **Who gets it?** _

_Marketing Hub Pro, Marketing Hub Enterprise, Operations Hub Pro, Operations Hub Enterprise, Sales Hub Pro, Sales Hub Enterprise, Service Hub Pro, Service Hub Enterprise_

> * * *

You can find the beta here:

 ![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/d/b/db66debfacbb84354a91132490254f42d910ce25.png)

If you want to make it easier for others to find this product update and beta, you can accept my reply as a solution. I’d appreciate it.

Have a great day!
