CRM

jcl_tg
Member

Help! Need to normalize date property to first of the month.

We have a contract end date property. The contract end date varies, but we need to send email notifications on the 20th of the month before. I'm trying to create a property to trigger the workflow off, but I seem to be running into issues.

 

I've outlined what I've tried below. Please let me know if you have other suggestions or edits to the code I've added. Thank you!!

 

Reviewed/Eliminated Solutions

  • Relational Dates:  I've tried adding the logic contract end date is less than 30 days from now and similar workarounds. Since both the contract end date and the number of days in a month vary, it was difficult to accurately add this logic.
  • Format Data: I tried reformatting the contract end date using this workflow function. Got as far as  minus_time([contract_end], 1, "months"), then datetimeformat([lease_end], "%m"). I wanted to create a calculated field, but this formatting function only exports as a string (both property have a datetime field type). I can't get it to export as anything other than a string, so I can't see a way to do a calculated field.
  • Custom Code: This section is well over my head. I've tried editing existing examples without much luck. I'm including what I think is my best attempt, but I'd love any suggestions you can provide!

 

Spoiler

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


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

const hubspotClient = new hubspot.Client({
accessToken: process.env.secretName
});

const contactId = event.object.objectId

const results = await hubspotClient.crm.contacts.basicApi.getById(contactId, [
'lease_end',
])

const contact = results.body.properties

const prepayUtilitiesDate = new Date(contact.prepay_utilities_request_date)

prepayUtilitiesDate.setMonth(closeDate.getMonth() - 1)
prepayUtilitiesDate.setDate(20)
prepayUtilitiesDate.setMilliseconds(0)
prepayUtilitiesDate.setSeconds(0)
prepayUtilitiesDate.setMinutes(0)
prepayUtilitiesDate.setHours(0)

const newContactData = {
properties: {
prepay_utilities_request_date: prepayUtilitiesDate.getTime,
}
}

const res = await hubspotClient.crm.contacts.basicApi.update(newContactData)


callback({
outputFields: {}
})
}

 

0 Upvotes
1 Reply 1
amitsarda
Participant

Help! Need to normalize date property to first of the month.

Hi @jcl_tg I think this will be very easily doable with an automation tool like Make/Integromat.

Apart from not wanting to sign up for another paid tool, I think this works beautifully and is less complex too.