Hello,
I am trying to write a custom code snippet in a workflow that would allow me to compare the create date to the close date of a deal to see if they’re equal. The reason for this is I would like to sort deals that have the same create and close date and have them be on a separate branch for the next step of the workflow. Any help regarding set-up or code editing would be appreciated. (For the API key I used the variable “API-Key” only for the example, the real API key comes from the private app)
Here is waht I have currently:
Language: node.js 16.x
Secret: Test
Properties:
createdate= Create date
closedate= Close date
Outputs:
Enumeration= hs_execution_state
API key: From private app - scope has: deal objects/ schema read/write, automation, actions, oauth
Code:
const hubspot = require(‘@hubspot/api-client’);
const hubspotClient = new hubspot.Client({ apiKey: ‘API-Key’ });
(async () => {
try {
// Initialize HubSpot API client
await hubspotClient.cms.intialize();
// Define properties
const createDate = “createdate”;
const closeDate = “closedate”;
// Fetch deals
const deals = await hubspotClient.crm.deals.getAll();
// Loop through each deal
for (const deal of deals.results) {
// Extract create date and close date
const createDate = deal.properties[createDate];
const closeDate = deal.properties[closeDate];
// Compare create date and close date
if (createDate === closeDate) {
// Create branch for deals where create date and close date are equal
console.log(`Deal ${deal.id}: Create date and Close date are equal`);
// … Create branch in HubSpot workflow for this deal
await hubspotClient.crm.workflows.createBranch(deal.id, “equal_date_branch”);
} else {
// Create branch for deals where create date and close date are not equal
console.log(`Deal ${deal.id}: Create date and Close date are not equal`);
// … Create branch in HubSpot workflow for this deal
await hubspotClient.crm.workflows.createBranch(deal.id, “not_equal_date_branch”);
}
}
} catch (error) {
console.error(‘Error fetching deals from HubSpot:’, error);
}
})();