{"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":"

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.<\/LI>
  • Creates a ticket to initiate order entry.<\/LI>
  • Automatically populates ticket fields with key deal data:
    • Earliest start date from the quote\u2019s line items<\/LI>
    • Company\u2019s internal NavigaID (custom company property)

      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>

         I have a secret token that has these scopes:
        • 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>

          Here is the code I am using: <\/STRONG><\/STRONG>

          const hubspot = require(‘@hubspot\/api-client’);<\/P>

          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”;<\/P>

          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 || [];<\/P>

          debugLog += `Quotes found: ${quotes.length}n`;<\/P>

          if (quotes.length === 0) {
          debugLog += “No quotes found for deal.n”;
          callback({
          outputFields: {
          earliest_start_date: null,
          debug_log: debugLog
          }
          });
          return;
          }<\/P>

          \/\/ Step 3: Use most recent quote
          const latestQuoteId = quotes[0].id;
          debugLog += `Using quote ID: ${latestQuoteId}n`;<\/P>

          \/\/ Step 4: Get line items for the quote
          const lineItemResults = await hs.crm.quotes.associationsApi.getAll(latestQuoteId, ‘line_items’);
          const lineItems = lineItemResults.results || [];<\/P>

          debugLog += `Line items found: ${lineItems.length}n`;<\/P>

          if (lineItems.length === 0) {
          debugLog += “No line items found for quote.n”;
          callback({
          outputFields: {
          earliest_start_date: null,
          debug_log: debugLog
          }
          });
          return;
          }<\/P>

          \/\/ 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))
          );<\/P>

          const startDates = fullLineItems
          .map(res => res.body.properties?.start_date)
          .filter(date => !!date)
          .sort();<\/P>

          if (startDates.length === 0) {
          debugLog += “No valid start dates found.n”;
          callback({
          outputFields: {
          earliest_start_date: null,
          debug_log: debugLog
          }
          });
          return;
          }<\/P>

          const earliest = startDates[0];
          debugLog += `Earliest start date: ${earliest}n`;<\/P>

          callback({
          outputFields: {
          earliest_start_date: earliest,
          debug_log: debugLog
          }
          });<\/P>

          } catch (error) {
          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":"

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