String date to date property in automation/workflow with custom code
SOLVE
Trying to convert a string (mm/dd/yy) to something HubSpot won't bark at.
My code:
exports.main = async (event, callback) => {
var ca_date = event.inputFields['ca_date']; ca_date = ca_date.split(' ').join('T'); console.log(ca_date); var date = new Date(ca_date); console.log('<br />' + date); date = date.getTime() / 1000; console.log('<br />' + date);
callback({ outputFields: { date: date } }); }
date is being output as datetime, tested the code, example output below:
and it seems to work, however when I try to use the "Copy Property Value" function within the workflow (to a datepicker property), all I get is this:
Event
Unable to update property because property value isn't valid
Tried to talk to HupSpot online support but I don't think they have the expertise that I'm looking for, they wanted me to export to excel then import, which defeats to purpose of a workflow/automation.
I've tried it with python also and have been racking my head trying to get this to work.
Any assistance from anyone would be greatly appreciated.
You might just be over thinking it! Have you tried the .parse() method?
var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);
This should output a unixtimestamp in milliseconds, which is compatable with HS. You may want to do alittle additional modifiation if the time is important:
var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);
// set time to midnight to be compatable with HS date property
const midnightUnix = unixTimeStamp.setHours(0,0,0); // date.setHours(hours,minutes,seconds);
console.log(midnightUnix);
String date to date property in automation/workflow with custom code
SOLVE
Really sorry for the confusion. It looks like HubSpot's custom code function for workflows may not support the moment.js library. In that case, you can try using JavaScript's built-in Date object to convert the string date to an ISO 8601 format.
Here we're creating a new Date object from the string date and then using the Date object's toISOString() method to format it as an ISO 8601 string. You can then set the isoDate variable as the output field value, which should be in the correct format for HubSpot.
You might just be over thinking it! Have you tried the .parse() method?
var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);
This should output a unixtimestamp in milliseconds, which is compatable with HS. You may want to do alittle additional modifiation if the time is important:
var ca_date = event.inputFields['ca_date'];
const unixTimeStamp = Date.parse(ca_date);
// set time to midnight to be compatable with HS date property
const midnightUnix = unixTimeStamp.setHours(0,0,0); // date.setHours(hours,minutes,seconds);
console.log(midnightUnix);
String date to date property in automation/workflow with custom code
SOLVE
Hi, it looks like the code you provided is converting the string date to a Unix timestamp (in seconds), which may not be compatible with HubSpot's date format. HubSpot expects a date value in ISO 8601 format, which is "yyyy-mm-ddThh:mm:ssZ".
You can modify your code to convert the string date to the ISO 8601 format before setting it as the output field value.
Here's an example implementation using the moment.js library:
const moment = require('moment');
exports.main = async (event, callback) => {
const ca_date = event.inputFields['ca_date'];
const date = moment(ca_date, 'MM/DD/YY').format('YYYY-MM-DDTHH:mm:ssZ');
console.log(date);
callback({
outputFields: {
date: date
}
});
}
In this implementation, we're using moment.js to parse the string date with the 'MM/DD/YY' format and format it as an ISO 8601 string using 'YYYY-MM-DDTHH:mm:ssZ'. You can then set the date variable as the output field value, which should be in the correct format for HubSpot. We always work in this manner at Triotech Systems. I hope this helps!
String date to date property in automation/workflow with custom code
SOLVE
Thanks for the tip. Unfortunately I couldn't get it to work. I'm using the custom code function in the workflow (node.js) and it complaining about moment.
I'll keep plugging away at it, just very frustrating as HubSpot hasn't provided any guidance. I'm able to use Zapier and use the formatter feature to do what it needs to do, but am trying to convert my zaps to workflows if possible.
String date to date property in automation/workflow with custom code
SOLVE
Really sorry for the confusion. It looks like HubSpot's custom code function for workflows may not support the moment.js library. In that case, you can try using JavaScript's built-in Date object to convert the string date to an ISO 8601 format.
Here we're creating a new Date object from the string date and then using the Date object's toISOString() method to format it as an ISO 8601 string. You can then set the isoDate variable as the output field value, which should be in the correct format for HubSpot.
String date to date property in automation/workflow with custom code
SOLVE
Thank you. I am not sure if my reply shows correctly, but I have the output correct in ISO timestamp. But I am not able to pass it to property. As it shows "invalid data"
Would the code you added correct this issue?
My issue is in this step:
The delivery date (timestamp) is a date picker property.
Hope you can help me, I am been 3 weeks stuck with this.