Getting undefined property error when working with HubSpot workflow API

I’m having trouble with HubSpot’s custom code feature in workflows. Every time I test my code I get an error about undefined properties. I’ve tried different approaches but keep running into the same issue.

The error message shows:

2023-03-22T17:30:09.266Z    INFO    debug checkpoint reached
2023-03-22T17:30:09.267Z    ERROR   TypeError: Cannot read properties of undefined (reading 'properties')
at /var/task/file.js:21:36
at processTicksAndRejections (node:internal/process/task_queues:96:5)

My debug message shows up fine but then it fails when trying to access the properties. Here’s what I’m working with:

// Working with Deal records to calculate dates
// Using custom fields: "Start Date", "End Date", "Duration"

const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {

// Initialize HubSpot client
    const hsClient = new hubspot.Client({
        accessToken: process.env.secretName
    });

// Get deal data with specific properties
hsClient.crm.deals.basicApi.getById(event.object.objectId, ["start_date","duration"])
.then(response => {

 // Extract property values

  console.log("debug checkpoint reached");
                                                             
     let beginDate = response.body.properties.start_date;
     let timespan = response.body.properties.duration;

  // Calculate end date
     let dateObj = new Date(beginDate)
     let durationInt = parseInt(timespan)
     let resultDate = new Date(dateObj.setDate(dateObj.getDate()+durationInt))
     resultDate = resultDate.getTime()

       callback({
         outputFields: {
           end_date: resultDate
         }
       });
     })
     .catch(error => {
       console.error(error);
     });
 }

I have the custom properties set up in HubSpot already. The goal is to grab existing values and calculate a new date from them.

I’ve hit this exact issue with HubSpot workflow APIs. The deal probably doesn’t have values for the properties you’re requesting, so the properties object comes back incomplete. HubSpot won’t include properties in the response if they’re empty. Add null checks before accessing them: if (response.body && response.body.properties) { let beginDate = response.body.properties.start_date || null; let timespan = response.body.properties.duration || null; if (beginDate && timespan) { // Your calculation logic here } else { console.log(“Missing required properties”); callback({ outputFields: {} }); return; } } Double-check your property names match exactly what’s in HubSpot. Internal names often differ from display names. Log the entire response.body object first to see what you’re actually getting back.

Had the same headaches with workflow custom code and this exact error. The problem’s probably that the API response structure isn’t what you’re expecting - especially with empty or missing property values. Don’t assume the response structure. Log the full response object first to see what’s actually coming back. Replace your property access with console.log(JSON.stringify(response, null, 2)) before line 21. You’ll probably find that response.body doesn’t exist or the properties are nested differently. Also check if your property names are the internal API names vs display names - HubSpot’s picky about this. Sometimes properties show up as hs_start_date internally when they display as start_date in the UI. Since your debug checkpoint works but properties fail, the API call’s succeeding but returning unexpected data structure.

you’re trying to access properties without checking if the response structure is right first. wrap your property access in a try-catch block, or check if response.body exists before using it. hubspot sometimes returns different response formats depending on the object’s state. also double-check you’re using the correct property internal names - they’re often different from what you see in the ui.