{"id":372,"date":"2025-05-02T22:45:29","date_gmt":"2025-05-02T22:45:29","guid":{"rendered":"http:\/\/www.soapandseife.com\/?p=372"},"modified":"2025-05-03T23:37:59","modified_gmt":"2025-05-03T23:37:59","slug":"custom-code-in-deal-workflow-to-create-ticket","status":"publish","type":"post","link":"http:\/\/www.soapandseife.com\/index.php\/2025\/05\/02\/custom-code-in-deal-workflow-to-create-ticket\/","title":{"rendered":"Custom Code in Deal Workflow to create ticket"},"content":{"rendered":"
But it the fields are coming back empty on the ticket.<\/P>
This is what is my logic:<\/P>
Take the deal ID (hs_object_id)<\/P><\/LI>
Fetch all quotes associated with the deal<\/P><\/LI>
Sort them to find the most recent quote<\/STRONG><\/P><\/LI> Retrieve the line items associated with that quote<\/STRONG><\/P><\/LI> Identify the earliest start date<\/STRONG><\/P><\/LI> Return it for use in the workflow<\/P> crm.objects.deals.read<\/P><\/LI> crm.objects.quotes.read<\/P><\/LI> crm.objects.line_items.read<\/P><\/LI> crm.schemas.line_items.read<\/P><\/LI> crm.objects.companies.read<\/P><\/LI> crm.associations.read<\/P> const hubspot = require(‘@hubspot\/api-client’);<\/P> exports.main = async (event, callback) => { try { debugLog += `Quotes found: ${quotes.length}n`;<\/P> if (quotes.length === 0) { \/\/ Step 3: Use most recent quote \/\/ Step 4: Get line items for the quote debugLog += `Line items found: ${lineItems.length}n`;<\/P> if (lineItems.length === 0) { \/\/ Step 5: Fetch full details of line items to access start_date const startDates = fullLineItems if (startDates.length === 0) { const earliest = startDates[0]; callback({ } catch (error) { 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…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/posts\/372"}],"collection":[{"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/comments?post=372"}],"version-history":[{"count":1,"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/posts\/372\/revisions"}],"predecessor-version":[{"id":373,"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/posts\/372\/revisions\/373"}],"wp:attachment":[{"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/media?parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/categories?post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.soapandseife.com\/index.php\/wp-json\/wp\/v2\/tags?post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
const hs = new hubspot.Client({ accessToken: process.env.Line_Item_API });
const dealId = event.inputFields[‘hs_object_id’];
let debugLog = “Step 1: Startn”;<\/P>
\/\/ Step 2: Get quotes associated with the deal
debugLog += “Step 2: Fetching associated quotes…n”;
const quoteResults = await hs.crm.deals.associationsApi.getAll(dealId, ‘quotes’);
const quotes = quoteResults.results || [];<\/P>
debugLog += “No quotes found for deal.n”;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
return;
}<\/P>
const latestQuoteId = quotes[0].id;
debugLog += `Using quote ID: ${latestQuoteId}n`;<\/P>
const lineItemResults = await hs.crm.quotes.associationsApi.getAll(latestQuoteId, ‘line_items’);
const lineItems = lineItemResults.results || [];<\/P>
debugLog += “No line items found for quote.n”;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
return;
}<\/P>
const fullLineItems = await Promise.all(
lineItems.map(item => hs.crm.lineItems.basicApi.getById(item.id))
);<\/P>
.map(res => res.body.properties?.start_date)
.filter(date => !!date)
.sort();<\/P>
debugLog += “No valid start dates found.n”;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
return;
}<\/P>
debugLog += `Earliest start date: ${earliest}n`;<\/P>
outputFields: {
earliest_start_date: earliest,
debug_log: debugLog
}
});<\/P>
debugLog += `Error: ${error.message || error.toString()}n`;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
}
};<\/P><\/DIV><\/LI><\/UL><\/DIV><\/DIV><\/LI><\/UL><\/DIV><\/LI><\/UL><\/LI><\/UL><\/DIV><\/DIV><\/DIV><\/DIV><\/DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"