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.