APIs & Integrations

MatBehr
Participant

Ticket workflow custom code to create associated note

SOLVE

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)
}}

 

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

 

0 Upvotes
2 Accepted solutions
MatBehr
Solution
Participant

Ticket workflow custom code to create associated note

SOLVE

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);
}
};

View solution in original post

0 Upvotes
karstenkoehler
Solution
Hall of Famer | Partner
Hall of Famer | Partner

Ticket workflow custom code to create associated note

SOLVE

Hi @marcodirk,

 

Good news, this is now possible!

 

Workflows
Create note workflow action
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:

 

karstenkoehler_1-1731043406854.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!

Karsten Köhler
HubSpot Freelancer | RevOps & CRM Consultant | Community Hall of Famer

Beratungstermin mit Karsten vereinbaren

 

Did my post help answer your query? Help the community by marking it as a solution.

View solution in original post

0 Upvotes
5 Replies 5
karstenkoehler
Solution
Hall of Famer | Partner
Hall of Famer | Partner

Ticket workflow custom code to create associated note

SOLVE

Hi @marcodirk,

 

Good news, this is now possible!

 

Workflows
Create note workflow action
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:

 

karstenkoehler_1-1731043406854.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!

Karsten Köhler
HubSpot Freelancer | RevOps & CRM Consultant | Community Hall of Famer

Beratungstermin mit Karsten vereinbaren

 

Did my post help answer your query? Help the community by marking it as a solution.

0 Upvotes
MatBehr
Solution
Participant

Ticket workflow custom code to create associated note

SOLVE

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);
}
};

0 Upvotes
MatBehr
Participant

Ticket workflow custom code to create associated note

SOLVE

MatBehr_0-1722543958886.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

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Ticket workflow custom code to create associated note

SOLVE

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


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes
MatBehr
Participant

Ticket workflow custom code to create associated note

SOLVE

Hi Jaycee, 

 

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

 

Thanks

 

MAt

0 Upvotes