# Custom Code in Deal Workflow to create ticket

**URL:** <https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995>\
**Category:** CMS Development\
**Tags:** html\
**Created:** [May 2, 2025, 10:45pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995 "2025-05-02T22:45:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![SRincon8](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SRincon8](https://community.hubspot.com/u/SRincon8)\
**Post date:** [May 2, 2025, 10:45pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/1 "2025-05-02T22:45:29Z")

</div>

Hello,

If someone can help me identify why my custom code is not working for a Deal Workflow to Create a Ticket and bring Ticket Properties I need.

We are building a custom HubSpot workflow that:

- Triggers when a quote is marked “Ready for Booking” and hasn’t been signed yet.
- Creates a ticket to initiate order entry.
- Automatically populates ticket fields with key deal data:
  - Earliest start date from the quote’s line items

  - Company’s internal NavigaID (custom company property)

---

<div class="post-metadata">

**Author:** ![DianaGomez](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dianagomez/32/48035_2.png) [@DianaGomez](https://community.hubspot.com/u/DianaGomez)\
**Post date:** [May 5, 2025, 8:35am UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/2 "2025-05-05T08:35:38Z")

</div>

Hi @SRincon8,

Thanks for reaching out to the Community!

I would like to invite some members of our community who may offer valuable insights.— hey @nickdeckerdevs1, @WesQ, **@skimura** - Could you share your advice with @SRincon8?

Thanks for taking a look!

Diana

---

<div class="post-metadata">

**Author:** ![WesQ](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wesq/32/17184_2.png) [@WesQ](https://community.hubspot.com/u/WesQ)\
**Post date:** [May 5, 2025, 9:39am UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/3 "2025-05-05T09:39:40Z")

</div>

Hi there! @SRincon8,

Can you give us more details?

1) What error message are you getting?

2) At what part of the logic are you having problems?

- Take the deal ID (hs\_object\_id)
- Fetch all quotes associated with the deal
- Sort them to find the **most recent quote**
- Retrieve the **line items associated with that quote**
- Identify the **earliest start date**
- Return it for use in the workflow

---

<div class="post-metadata">

**Author:** ![nickdeckerdevs1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/nickdeckerdevs1/32/5198_2.png) [@nickdeckerdevs1](https://community.hubspot.com/u/nickdeckerdevs1)\
**Post date:** [May 5, 2025, 1:46pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/4 "2025-05-05T13:46:20Z")

</div>

Excatly -- we have to understand what is going wrong here before we really determine the issue or we are wild goose chasing.. I like wild goose chasing on a Friday, never a Monday.

---

<div class="post-metadata">

**Author:** ![SRincon8](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SRincon8](https://community.hubspot.com/u/SRincon8)\
**Post date:** [May 7, 2025, 5:28pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/5 "2025-05-07T17:28:51Z")

</div>

Sorry about that.

I am not gettting any errors. The deal enrolls in the workflow and created the ticket, however it does not bring any infomration on the Deal Property Field: Earliest Line Item Start Date.

i have another Property Deal Created Debug text to tell me what is happening and this is the result: Step 1: Start  
Quotes found: Step 1: Start  
Quotes found: 0

Yet, there is a quote in Piublished status and the line items from the Quote are showing in the line items in the deal.

These are the event details of the code:

eturn value

[{“fieldKey”:“earliest\_start\_date”,“value”:“”},{“fieldKey”:“company\_navigaid”,“value”:“”},{“fieldKey”:“debug\_text”,“value”:“Step 1: Start\nQuotes found: 0”}]

Yes, the company\_navigaid is showing up Blank too in the Deal Property.

Does this help?

---

<div class="post-metadata">

**Author:** ![SRincon8](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SRincon8](https://community.hubspot.com/u/SRincon8)\
**Post date:** [May 7, 2025, 5:34pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/6 "2025-05-07T17:34:17Z")

</div>

Sorry about that.

I am not gettting any errors. The deal enrolls in the workflow and created the ticket, however it does not bring any infomration on the Deal Property Field: Earliest Line Item Start Date.

i have another Property Deal Created Debug text to tell me what is happening and this is the result: Step 1: Start  
Quotes found: Step 1: Start  
Quotes found: 0

Yet, there is a quote in Piublished status and the line items from the Quote are showing in the line items in the deal.

These are the event details of the code:

eturn value

[{“fieldKey”:“earliest\_start\_date”,“value”:“”},{“fieldKey”:“company\_navigaid”,“value”:“”},{“fieldKey”:“debug\_text”,“value”:“Step 1: Start\nQuotes found: 0”}]

Yes, the company\_navigaid is showing up Blank too in the Deal Property.

Does this help?

Here is the code again, I did some updated, but still experiencing the same results:

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

exports.main = async (event, callback) =\> {  
const client = new hubspot.Client({ accessToken: process.env.Line\_Item\_API });  
const dealId = event.inputFields[‘hs\_object\_id’];  
let debug = [];

try {  
debug.push(“Step 1: Start”);

// STEP 2: Get quotes associated with the deal  
const quoteAssoc = await client.apiRequest({  
method: ‘GET’,  
path: `/crm/v4/objects/deals/${dealId}/associations/quotes`  
});

const quoteIds = quoteAssoc.body?.results?.map(r =\> r.toObjectId) || [];  
debug.push(`Quotes found: ${quoteIds.length}`);

if (quoteIds.length === 0) {  
callback({ outputFields: { earliest\_start\_date: ‘’, company\_navigaid: ‘’, debug\_text: debug.join(‘\n’) } });  
return;  
}

// STEP 3: Pick the most recent quote based on createdate  
const quoteBatch = await client.crm.quotes.batchApi.read({  
inputs: quoteIds.map(id =\> ({ id })),  
properties: [‘createdate’]  
});

const sortedQuotes = quoteBatch.results  
.map(q =\> ({  
id: q.id,  
createdAt: new Date(q.properties.createdate)  
}))  
.sort((a, b) =\> b.createdAt - a.createdAt);

const latestQuoteId = sortedQuotes[0].id;  
debug.push(`Latest quote ID: ${latestQuoteId}`);

// STEP 4: Get line items from the quote  
const lineItemAssoc = await client.apiRequest({  
method: ‘GET’,  
path: `/crm/v4/objects/quotes/${latestQuoteId}/associations/line_items`  
});

const lineItemIds = lineItemAssoc.body?.results?.map(r =\> r.toObjectId) || [];  
debug.push(`Line items found: ${lineItemIds.length}`);

if (lineItemIds.length === 0) {  
callback({ outputFields: { earliest\_start\_date: ‘’, company\_navigaid: ‘’, debug\_text: debug.join(‘\n’) } });  
return;  
}

// STEP 5: Get each line item and extract Start\_Date  
const lineItems = await client.crm.lineItems.batchApi.read({  
inputs: lineItemIds.map(id =\> ({ id })),  
properties: [‘Start\_Date’]  
});

const startDates = lineItems.results  
.map(item =\> item.properties?.Start\_Date)  
.filter(Boolean)  
.map(date =\> new Date(date));

if (startDates.length === 0) {  
debug.push(“No valid Start\_Date values found.”);  
callback({ outputFields: { earliest\_start\_date: ‘’, company\_navigaid: ‘’, debug\_text: debug.join(‘\n’) } });  
return;  
}

const earliest = new Date(Math.min(…startDates)).toISOString().split(‘T’)[0];  
debug.push(`Earliest start date: ${earliest}`);

// STEP 6: Get associated company and NavigaID  
const companyAssoc = await client.apiRequest({  
method: ‘GET’,  
path: `/crm/v4/objects/deals/${dealId}/associations/companies`  
});

const companyIds = companyAssoc.body?.results?.map(r =\> r.toObjectId) || [];

let navigaid = ‘’;  
if (companyIds.length \> 0) {  
const company = await client.crm.companies.basicApi.getById(companyIds[0], [‘navigaid’]);  
navigaid = company.properties.navigaid || ‘’;  
debug.push(`NavigaID found: ${navigaid}`);  
} else {  
debug.push(‘No company associated.’);  
}

// STEP 7: Final output  
callback({  
outputFields: {  
earliest\_start\_date: earliest,  
company\_navigaid: navigaid,  
debug\_text: debug.join(‘\n’)  
}  
});

} catch (err) {  
debug.push(`ERROR: ${err.message}`);  
callback({  
outputFields: {  
earliest\_start\_date: ‘’,  
company\_navigaid: ‘’,  
debug\_text: debug.join(‘\n’)  
}  
});  
}  
};

---

<div class="post-metadata">

**Author:** ![nickdeckerdevs1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/nickdeckerdevs1/32/5198_2.png) [@nickdeckerdevs1](https://community.hubspot.com/u/nickdeckerdevs1)\
**Post date:** [May 7, 2025, 5:49pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/7 "2025-05-07T17:49:44Z")

</div>

On mobile so difficult to debug.  
to confirm earliest time a not being output, that is the problem?

can you just declare earliest at the start of your workflow main function and that resolves this? Or set earliest in the output ?

---

<div class="post-metadata">

**Author:** ![SRincon8](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SRincon8](https://community.hubspot.com/u/SRincon8)\
**Post date:** [May 7, 2025, 6:14pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/8 "2025-05-07T18:14:33Z")

</div>

It is the earliest Start Date of an line item. A quote may have more than 1 line item and each has their own Start Date. I want to get the Start Date of the line items thats has the earliest Start Date of all the line items.

---

<div class="post-metadata">

**Author:** ![nickdeckerdevs1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/nickdeckerdevs1/32/5198_2.png) [@nickdeckerdevs1](https://community.hubspot.com/u/nickdeckerdevs1)\
**Post date:** [May 7, 2025, 7:14pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/9 "2025-05-07T19:14:36Z")

</div>

Well, first lets figure out what is being returned from this batch call

```lia-code-sample
const lineItems = await client.crm.lineItems.batchApi.read({
 inputs: lineItemIds.map(id => ({ id })),
 properties: ['Start_Date']
});

debug.push(`Line Items Found: ${JSON.stringify(lineItems.results)}`);

const startDates = lineItems.results
 .map(item => item.properties?.Start_Date)
 .filter(Boolean)
 .map(date => new Date(date));

```

I don’t use the hubspot sdk, but all properties are stored as lowercase inside HubSpot, not sure if the sdk translates that -- but we also push this information so we can make sure we have actual data going into the map/filter/map  
I’m thinking the code may be more like this:

```lia-code-sample
const lineItems = await client.crm.lineItems.batchApi.read({
 inputs: lineItemIds.map(id => ({ id })),
 properties: ['start_date']
});

debug.push(`Line Items Found: ${JSON.stringify(lineItems.results)}`);

const startDates = lineItems.results
 .map(item => item.properties?.start_date)
 .filter(Boolean)
 .map(date => new Date(date));

```

---

<div class="post-metadata">

**Author:** ![SRincon8](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SRincon8](https://community.hubspot.com/u/SRincon8)\
**Post date:** [May 7, 2025, 7:44pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/10 "2025-05-07T19:44:22Z")

</div>

I added more debug and now I get this error:  
Deal ID: 36858518450 ❌ Error: hubspotClient.crm.associations.v4.basicApi.getAll is not a function Stack: TypeError: hubspotClient.crm.associations.v4.basicApi.getAll is not a function at exports.main (/var/task/file.js:11:80) at exports.hubspot\_handler [as handler] (/var/task/hubspotHandler.js:7:21) at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)

I got earlier error with: associatiionsAPI  
Deal ID: 36858514850 ❌ Error: Cannot read properties of undefined (reading ‘getAll’) Stack: TypeError: Cannot read properties of undefined (reading ‘getAll’) at exports.main (/var/task/file.js:12:77) at exports.hubspot\_handler [as handler] (/var/task/hubspotHandler.js:7:21) at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)

I can’t get passed this.

---

<div class="post-metadata">

**Author:** ![nickdeckerdevs1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/nickdeckerdevs1/32/5198_2.png) [@nickdeckerdevs1](https://community.hubspot.com/u/nickdeckerdevs1)\
**Post date:** [May 7, 2025, 7:53pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/11 "2025-05-07T19:53:45Z")

</div>

Did you add what I suggested or did you change other code?  
I don’t see “hubspotClient.crm.associations.v4.basicApi.getAll” in any of the code you have supplied, that is likely not a proper way to pull in associations, if it is saying “is not a function”  
Can you use the code below, and then paste out what is in the debug that gets output if there are errors?

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {
 const client = new hubspot.Client({ accessToken: process.env.Line_Item_API });
 const dealId = event.inputFields['hs_object_id'];
 let debug = [];

 try {
 debug.push("Step 1: Start");

 // STEP 2: Get quotes associated with the deal
 const quoteAssoc = await client.apiRequest({
 method: 'GET',
 path: `/crm/v4/objects/deals/${dealId}/associations/quotes`
 });

 const quoteIds = quoteAssoc.body?.results?.map(r => r.toObjectId) || [];
 debug.push(`Quotes found: ${quoteIds.length}`);

 if (quoteIds.length === 0) {
 callback({ outputFields: { earliest_start_date: '', company_navigaid: '', debug_text: debug.join('\n') } });
 return;
 }

 // STEP 3: Pick the most recent quote based on createdate
 const quoteBatch = await client.crm.quotes.batchApi.read({
 inputs: quoteIds.map(id => ({ id })),
 properties: ['createdate']
 });

 const sortedQuotes = quoteBatch.results
 .map(q => ({
 id: q.id,
 createdAt: new Date(q.properties.createdate)
 }))
 .sort((a, b) => b.createdAt - a.createdAt);

 const latestQuoteId = sortedQuotes[0].id;
 debug.push(`Latest quote ID: ${latestQuoteId}`);

 // STEP 4: Get line items from the quote
 const lineItemAssoc = await client.apiRequest({
 method: 'GET',
 path: `/crm/v4/objects/quotes/${latestQuoteId}/associations/line_items`
 });

 const lineItemIds = lineItemAssoc.body?.results?.map(r => r.toObjectId) || [];
 debug.push(`Line items found: ${lineItemIds.length}`);

 if (lineItemIds.length === 0) {
 callback({ outputFields: { earliest_start_date: '', company_navigaid: '', debug_text: debug.join('\n') } });
 return;
 }

 // STEP 5: Get each line item and extract Start_Date
 const lineItems = await client.crm.lineItems.batchApi.read({
 inputs: lineItemIds.map(id => ({ id })),
 properties: ['start_date']
 });

 debug.push(`Line Items Found: ${JSON.stringify(lineItems)}`);

 const startDates = lineItems.results
 .map(item => item.properties?.start_date)
 .filter(Boolean)
 .map(date => new Date(date));

 if (startDates.length === 0) {
 debug.push("No valid Start_Date values found.");
 callback({ outputFields: { earliest_start_date: '', company_navigaid: '', debug_text: debug.join('\n') } });
 return;
 }

 const earliest = new Date(Math.min(...startDates)).toISOString().split('T')[0];
 debug.push(`Earliest start date: ${earliest}`);

 // STEP 6: Get associated company and NavigaID
 const companyAssoc = await client.apiRequest({
 method: 'GET',
 path: `/crm/v4/objects/deals/${dealId}/associations/companies`
 });

 const companyIds = companyAssoc.body?.results?.map(r => r.toObjectId) || [];

 let navigaid = '';
 if (companyIds.length > 0) {
 const company = await client.crm.companies.basicApi.getById(companyIds[0], ['navigaid']);
 navigaid = company.properties.navigaid || '';
 debug.push(`NavigaID found: ${navigaid}`);
 } else {
 debug.push('No company associated.');
 }

 // STEP 7: Final output
 callback({
 outputFields: {
 earliest_start_date: earliest,
 company_navigaid: navigaid,
 debug_text: debug.join('\n')
 }
 });

 } catch (err) {
 debug.push(`ERROR: ${err.message}`);
 callback({
 outputFields: {
 earliest_start_date: '',
 company_navigaid: '',
 debug_text: debug.join('\n')
 }
 });
 }
};

```

---

<div class="post-metadata">

**Author:** ![SRincon8](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SRincon8](https://community.hubspot.com/u/SRincon8)\
**Post date:** [May 7, 2025, 8:57pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/12 "2025-05-07T20:57:24Z")

</div>

This is what i get now

 ![Screenshot 2025-05-07 at 4.55.33 PM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/5/2/529db35cd2cd40d7de93c0fdf6dcac2f94f1ce92.png)

---

<div class="post-metadata">

**Author:** ![nickdeckerdevs1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/nickdeckerdevs1/32/5198_2.png) [@nickdeckerdevs1](https://community.hubspot.com/u/nickdeckerdevs1)\
**Post date:** [May 7, 2025, 9:06pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/13 "2025-05-07T21:06:35Z")

</div>

So this is the part that isn’t finding assocated quotes. I don’t see any v4 -- Are you using AI to generate this? AI is super unreliable -- I would manually test out these api calls inside something like postman or locally from your machine before putting them into a workflow. there is no v4 for [deals api](https://developers.hubspot.com/docs/reference/api/crm/objects/deals/)  
[https://api.hubapi.com/crm/v3/objects/deals/{dealId}](https://api.hubapi.com/crm/v3/objects/deals/%7BdealId%7D)

```lia-code-sample
const quoteAssoc = await client.apiRequest({
 method: 'GET',
 path: `/crm/v4/objects/deals/${dealId}/associations/quotes`
 });

```

---

<div class="post-metadata">

**Author:** ![SRincon8](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SRincon8](https://community.hubspot.com/u/SRincon8)\
**Post date:** [May 8, 2025, 8:00pm UTC](https://community.hubspot.com/t/custom-code-in-deal-workflow-to-create-ticket/130995/14 "2025-05-08T20:00:41Z")

</div>

I was abel to resolve it. Thank you all for your feedback here. I appreciate it. I ended up using getPage and that did it.
