Custom Code in Deal Workflow to create ticket
- by Admin
- Posted on May 2, 2025
- 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)
But it the fields are coming back empty on the ticket.
This is what is my logic:
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
I have a secret token that has these scopes:crm.objects.deals.read
crm.objects.quotes.read
crm.objects.line_items.read
crm.schemas.line_items.read
crm.objects.companies.read
crm.associations.read
Here is the code I am using:const hubspot = require(‘@hubspot/api-client’);
exports.main = async (event, callback) => {
const hs = new hubspot.Client({ accessToken: process.env.Line_Item_API });
const dealId = event.inputFields[‘hs_object_id’];
let debugLog = “Step 1: Startn”;try {
// 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 || [];debugLog += `Quotes found: ${quotes.length}n`;
if (quotes.length === 0) {
debugLog += “No quotes found for deal.n”;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
return;
}// Step 3: Use most recent quote
const latestQuoteId = quotes[0].id;
debugLog += `Using quote ID: ${latestQuoteId}n`;// Step 4: Get line items for the quote
const lineItemResults = await hs.crm.quotes.associationsApi.getAll(latestQuoteId, ‘line_items’);
const lineItems = lineItemResults.results || [];debugLog += `Line items found: ${lineItems.length}n`;
if (lineItems.length === 0) {
debugLog += “No line items found for quote.n”;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
return;
}// Step 5: Fetch full details of line items to access start_date
const fullLineItems = await Promise.all(
lineItems.map(item => hs.crm.lineItems.basicApi.getById(item.id))
);const startDates = fullLineItems
.map(res => res.body.properties?.start_date)
.filter(date => !!date)
.sort();if (startDates.length === 0) {
debugLog += “No valid start dates found.n”;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
return;
}const earliest = startDates[0];
debugLog += `Earliest start date: ${earliest}n`;callback({
outputFields: {
earliest_start_date: earliest,
debug_log: debugLog
}
});} catch (error) {
debugLog += `Error: ${error.message || error.toString()}n`;
callback({
outputFields: {
earliest_start_date: null,
debug_log: debugLog
}
});
}
};
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…