Custom coded action not working: 'Days between property X & Y'

Spoiler

const hubspot = require(‘@hubspot/api-client’);

const hubspotClient = new hubspot.Client({

accessToken: process.env.PrivateAppToken

});

exports.main = (event, callback) => {

hubspotClient.crm.contacts.basicApi.getById(event.object.objectId)

.then(contact => {

var startDate = new Date(contact.properties.hs_lifecyclestage_marketingqualifiedlead_date.value);

var endDate = new Date(contact.properties.hs_lifecyclestage_opportunity_date.value);

var startTime = startDate.getTime();

var endTime = endDate.getTime();

var differenceInMilliseconds = endTime - startTime;

var differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24);

hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {

properties: {

daysBetween: differenceInDays

}

});

callback({

outputFields: {

output: differenceInDays

}

});

})

.catch(err => {

console.error(err);

throw err;

});

}

Hi all,

I am creating a custom coded action which displays the difference between the ‘Became MQL date’ & the ‘Became Opportunity date’ properties in full days. I don’t seem to understand why my code does not work. Check the spoiler for the code.

Hi @Raoul

Thank you for reaching out.

I want to tag some of our experts here - @CMcKay @jonchim @stefen do you have some suggestions for @Raoul on this?

Thank you!

Best

Tiphaine

Hey @Raoul and @TitiCuisset,

It’s difficult to say exactly what the issue might be without more context. One thing to check is whether the event object has an object property that has an objectId property. If either of these is null or undefined, it will cause an error when you try to access them.

Additionally, if the contact object does not have a properties property, or if the hs_lifecyclestage_marketingqualifiedlead_date or hs_lifecyclestage_opportunity_date properties are null or undefined, it will cause an error when you try to access their value property.

You might want to consider adding some error handling to your code to catch and handle these possible errors. For example, you could add a try-catch block around the code that attempts to access the event.object.objectId and contact.properties properties, and add appropriate error handling logic within the catch block.

Also while the math for the differenceInDays variable looks correct, the number will come out as a decimal, I would suggest you round that final number.

Let me know if this helps, and with a little more context I think we can solve this.

Also I wrote out this same formula in nodejs but I assume you’re wanting it in python

Carter McKay