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?