Getting undefined error when accessing object properties in HubSpot workflow API

I’m working with HubSpot workflows and trying to use their custom code actions. Every time I run my script I get an error about undefined properties. I’ve been testing with a basic example but still can’t get it working.

The error message shows:

2023-03-22T17:30:09.266Z    INFO    ******* reached checkpoint
2023-03-22T17:30:09.267Z    ERROR   TypeError: Cannot read properties of undefined (reading 'properties')
at /var/task/file.js:21:36

I added some debug logging and can see the code runs up to a certain point, then fails when trying to access the properties object. Here’s my test code:

// This example works with Deal records and calculates dates
// Requires custom properties: "Start Date", "End Date", "Duration"

const hubspotAPI = require('@hubspot/api-client');

exports.main = (eventData, callbackFn) => {

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

    // Fetch deal data using the object ID
    hsClient.crm.deals.basicApi.getById(eventData.object.objectId, ["start_date", "duration"])
    .then(response => {
        
        console.log("******* reached checkpoint");
        
        // Extract values from response
        let beginDate = response.body.properties.start_date;
        let durationDays = response.body.properties.duration;
        
        // Calculate end date
        let startingDate = new Date(beginDate);
        let dayCount = parseInt(durationDays);
        let endingDate = new Date(startingDate.setDate(startingDate.getDate() + dayCount));
        let finalTimestamp = endingDate.getTime();
        
        callbackFn({
            outputFields: {
                end_date: finalTimestamp
            }
        });
    })
    .catch(error => {
        console.error(error);
    });
}

The goal is to retrieve some property values and store them in variables for calculation. What could be causing this undefined properties error?

sounds like a timing issue or malformed response. wrap the property access in a try-catch block and log response.body before accessing properties. hubspot sometimes returns empty objects when records don’t have those fields set. also double-check you’re using the right property names - internal names vs display names are different in hubspot.

Hit this exact problem last month with HubSpot’s custom code actions. The issue is that the API response structure isn’t always what you expect, especially with properties that might not exist on the record. The error happens because response.body might be undefined or the properties object doesn’t have the fields you’re requesting. Add some defensive checks before accessing properties. Log the entire response object first to see what’s actually coming back. This usually happens when the deal record doesn’t have values for the properties you want, or when the property names don’t match exactly. Double-check that “start_date” and “duration” are the correct internal names for your custom properties in HubSpot. Display names often differ from internal property names. Also check if your access token has proper scopes for reading deal properties. I’ve seen API calls succeed but return limited data because of permission restrictions.