Created
January 1, 2023 22:11
-
-
Save TheWebTech/f202149a6ac91d258a4a15e4db90fe6c to your computer and use it in GitHub Desktop.
Custom Code Workflow action to create a note on the current contact a year from the date they became a subscriber.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
exports.main = async (event, callback) => { | |
// Pass in our private app token as a secret. | |
// https://developers.hubspot.com/docs/api/workflows/custom-code-actions#secrets | |
const privateAppToken = process.env.privateApp; | |
// Pass in properties to your custom coded action. | |
// https://developers.hubspot.com/docs/api/workflows/custom-code-actions#add-hubspot-properties-to-your-custom-code | |
const contact_id = event.inputFields["contact_id"]; | |
const subscriber_date = parseInt(event.inputFields["subscriber_date"]); | |
// add one year in epoch format to the subscriber date. | |
const subscriber_date_plus_one_year = subscriber_date + (31556926 * 1000); | |
// adding to subscriber date vs current date ensures even if the workflow timing isn't exact, the note for the date is. | |
// We use the "Basic" create endpoint for notes. As documented here: https://developers.hubspot.com/docs/api/crm/notes | |
let url = "https://api.hubapi.com/crm/v3/objects/notes"; | |
// a properties object is a required parameter so we create the object, the timestamp is what date we want to tie this note to. | |
// The note body is the actual message that will be visible in the contact record. | |
const properties = { | |
"hs_timestamp": subscriber_date_plus_one_year, | |
"hs_note_body": "This user has been subscribed for 1 full year!" | |
}; | |
/* On the overview tab we can see there's an association object we can pass when creating a new note. This enables us to associate | |
the note to a specific contact. The associationTypeId can be retrieved via the associations API.(https://developers.hubspot.com/docs/api/crm/associations) | |
The id for notes to contacts we received from the API was 202. | |
*/ | |
const associations = [ | |
{ | |
"to": { | |
"id": contact_id | |
}, | |
"types": [ | |
{ | |
"associationCategory": "HUBSPOT_DEFINED", | |
"associationTypeId": 202 | |
} | |
] | |
} | |
]; | |
// Make our API call to create the note. | |
axios | |
.post( | |
url, | |
{ | |
"properties": properties, | |
"associations": associations, | |
}, | |
{ | |
"headers": { | |
"Authorization": `Bearer ${privateAppToken}`, | |
"Content-Type": "application/json", | |
}, | |
} | |
) | |
.then((response) => { | |
console.log(response.data); | |
return response.data; | |
}) | |
.catch(function (error) { | |
console.log(error.toJSON()); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment