Changing Close Time to Decimal Hours

Hello,

I am trying to using a workflow with a custom code snippet in order to change the ticket close time property over into decimal time. The process that I have so far is using a “Format data” action that changes the time to close of a ticket into a unix time (String), and then using a custom code snippet that will then turn that unix time into decimal hours (Number). I think the errors that I am experiencing might be around how I input the variable at the start of the code and the fact that I am attempting to turn a string into a number value. Here are some screenshots and an export of the code, if there is anything else that you think would be helpful to know then feel free to ask. Thanks

exports.main = async (event, callback) => {
const unixTime = event.inputfields[‘time_to_close_unix’];
const date = new Date(unixTime * 1000);
const secondsSinceStartOfDay = (date.getHours() * 3600) + (date.getMinutes() * 60) + date.getSeconds();
const closeDecimalHours = secondsSinceStartOfDay / 3600;
callback ({ outputFields: { number: closeDecimalHours } } ) ; }

Hey, @TSweet2 :waving_hand: Can you share the error(s) you are receiving? This may help the community to better understand your issue.

If the community trues to reproduce this, outside the code and the custom ticket property, is there anything else that’s vital to include? It doesn’t look like it. It seems pretty straightforward.

Thanks! — Jaycee

Hi, @TSweet2. Thanks for adding the error body. One additional question – what are the type and fieldType for your custom property? You can verify it with this call:

https://api.hubapi.com/crm/v3/properties/tickets/time_to_close_unix?archived=false

Thanks! — Jaycee

@Jaycee_Lewis Where would I go about running this code snippet? I apologize if this is an easy question, I am still new operations in Hubspot

Hey @Jaycee_Lewis , I think the errors are relating to the property fields. Let me know if you need any other information.

The event error that I got was:
The custom code threw an error. This could be due to a syntax error in the code, an error being returned by an API, or some other issue with the code.

The log error was:

ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'time_to_close_unix')","reason":{"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'time_to_close_unix')","stack":["TypeError: Cannot read properties of undefined (reading 'time_to_close_unix')"," at Object.exports.main (/var/task/file.js:3:26)"," at Runtime.exports.hubspot_handler [as handler] (/var/task/hubspotHandler.js:6:21)"," at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1085:29)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'time_to_close_unix')"," at process.<anonymous> (file:///var/runtime/index.mjs:1188:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:140:20)"," at processPromiseRejections (node:internal/process/promises:274:27)"," at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}
Unknown application error occurred
Runtime.Unknown

Memory: 58/128 MB
Runtime: 75.67 ms