Issue in creating a workflow using custom code automations

Hi,

I am facing an issue in running a code which was provided in Hubspot Custom Code automations example. here-Data Hub Use Case Library

I am specifically trying to implement- https://github.com/HubSpot/sample-workflow-custom-code/blob/main/samples/calculate_contract_end_date.js

after a an automation-https://github.com/HubSpot/sample-workflow-custom-code/blob/main/samples/create_renewal_deal_and_associate_with_company.js takes place so we can calculate the renewal date which is one day after the contract end date. I don’t know node js at all.

Below is the code I currently have with the inputs and output defined. But when I create the output end_date and copy it to the property or the field in deals but it doesn’t work. Can anyone help regarding this?

// The following snippet applies to the Deal object, but can be modified to calculate a property on any Object
// This snippet requires 3 custom properties created in HubSpot:
// “Start Date” (date) / “End Date” (date) / “Duration” (number)

const hubspot = require(‘@hubspot/api-client’);
exports.main = (event, callback) => {

// Set up the HubSpot API client
const hubspotClient = new hubspot.Client({
accessToken: process.env.table_bi
});

// Use the client to pull information relating to the currently enrolled deal
hubspotClient.crm.deals.basicApi.getById(event.object.objectId, [“start_date”,“duration”,])
.then(results => {

// Store properties in variables
let startDate = results.body.properties.start_date;
let duration = results.body.properties.duration;

// Calculate the end date from the variables input
let d = new Date(startDate)
let i = parseInt(duration)
let end_date= new Date(d.setDate(d.getDate()+i))
end_date= end_date.getTime()

callback({
// Store the calculated date in a workflow output - don’t forget to copy that value to your “End date” property
outputFields: {
end_date: end_date
}
});
})
.catch(err => {
console.error(err);
});
}

Are you seeing any errors? I copied your code exactly and it worked on mine? At the bottom of the custom action there is the ability to test it against a record. Did you try that and are there any errors in the logs?

Hi, the script is working but not able to get the output in the deal object as a property as I am defining the the workflow. It says the end_date is not defined in the code but callback clearly defines end_date as output. I am also copying the data into the appropriate property as seen in below ss? What could be going wrong?

Ah, that yellow warning is key where it says “Not defined in code”. So you must have something not exactly right. Not defined in code would mean where you have this code:

callback({
 outputFields: {
 end_date: end_date
 }
});

You must have something other than end_date: end_date. Like if you had enddate: end_date I’d expect it to say not defined in code. It’s telling you the callback isn’t returning a property called end_date. So make sure there is no typo there or you didn’t accidentally change something. Also the test should have a value next to it. It needs ot look something like this:

Like you can see here below-

the end_date:end_date is as you specified. All the code is above, where are making a mistake because I can’t find one? I am giving it as an input after creating properties too.

Are you sure you are testing with a deal that has a value set for start date and duration? When I test mine with a deal without a start date or duration I get the same result as you. If you are testing with a deal with a start date and duration are the fields date and number respectively?

Tag relevant people please would love your opinions.