I need to create a workflow to transfer a date from text field property to a date picker field property.
The scenario is I have a date coming from a integration and unfortunately that integration can’t talk with date picker fields so I’ve had to have the date put into a text field.
The date going into the text field is in the format YYYY-MM-DD
Which I believe is the correct format for a hubspot date picker field.
I know I need to do a workflow with custom code but I’m having some issues getting it working.
Below is the code I currently have in the workflow - no errors are coming up but the data does not transfer to the date picker field - I would appreciate it if someone could help out, please!
exports.main = async (event) => {
// Input field: 'getaccept_estimated_delivery_date' contains the date in 'YYYY-MM-DD' format
const inputText = event.inputFields['getaccept_estimated_delivery_date'];
try {
// Log the input date for debugging
console.log('Input Date:', inputText);
// Convert the input text into a JavaScript Date object
const parsedDate = new Date(inputText);
if (isNaN(parsedDate.getTime())) {
throw new Error("Invalid date format");
}
// Format the date to YYYY-MM-DD
const year = parsedDate.getFullYear();
const month = ("0" + (parsedDate.getMonth() + 1)).slice(-2); // Months are zero-based
const day = ("0" + parsedDate.getDate()).slice(-2);
const formattedDate = `${year}-${month}-${day}`;
// Log the formatted date for debugging
console.log('Formatted Date:', formattedDate);
// Return the formatted date to the 'estimated_delivery_date' property
return {
outputFields: {
'estimated_delivery_date': formattedDate
}
};
} catch (error) {
console.error('Error processing the date:', error.message);
return {
outputFields: {
'estimated_delivery_date': null // Handle error accordingly
}
};
}
};
The issue is that the value you are supplying to the “Date()” constructor is a string value and it expects an integer value. Which results in an error. To address this you can use “parseInt()”.
Can you try the below code and let me know how you get on:
exports.main = async (event, callback) => {
// Input field: 'getaccept_estimated_delivery_date' contains the date in 'YYYY-MM-DD' format
const inputText = event.inputFields['getaccept_estimated_delivery_date'];
try {
// Log the input date for debugging
console.log('Input Date:', inputText);
// Convert the input text into a JavaScript Date object
const parsedDate = new Date(parseInt(inputText));
if (isNaN(parsedDate.getTime())) {
throw new Error("Invalid date format");
}
// Format the date to YYYY-MM-DD
const year = parsedDate.getFullYear();
const month = ("0" + (parsedDate.getMonth() + 1)).slice(-2); // Months are zero-based
const day = ("0" + parsedDate.getDate()).slice(-2);
const formattedDate = `${year}-${month}-${day}`;
// Log the formatted date for debugging
console.log('Formatted Date:', formattedDate);
// Return the formatted date to the 'estimated_delivery_date' property
callback({
outputFields: {
estimated_delivery_date: formattedDate
}
});
} catch (error) {
console.error('Error processing the date:', error.message);
callback({
outputFields: {
estimated_delivery_date: null
}
});
}
};
When testing using the code above, my Contact has a date property with value “04/09/2024” and when the code executes it copies to a single line text field with value “2024-09-04”. See screenshot below:
You need to convert the date to a timestamp in milliseconds
const parsedDate = new Date(inputText);
const dateToParse = parsedDate.getTime();
Now you can send this dateToParse in HubSpot.
I hope this will help you out. Please mark it as Solution Accepted and Upvote to help another Community member.
Thanks!