Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
jackcoldrick90 / associateContactToCompany.js
Last active October 7, 2024 20:53
This custom code snippet can be used to associate a contact to a company based on the company name property that is stored at a contact level. It's particularly useful if your customers are using a freemail address and aren't supplying a company website - just the name of their company. If no company is found a new record will be created in the …
// Import the Hubspot NodeJS Client Library - this will allow us to use the HubSpot APIs
const hubspot = require('@hubspot/api-client');
/*
This function is called when the custom code action is executed. It takes 2 arguements. The first is the event object which contains information on the currently enrolled object.
The second is the callback function which is used to pass data back to the workflow.
*/
exports.main = (event, callback) => {
// Instantiate a new HubSpot API client using the HAPI key (secret)
/* Example 2: Make a HTTP Request to Kickbox Email Verification API using NodeJS axios */
//1. Import libraries, in this instance "axios" to make HTTP requests
const axios = require('axios');
exports.main = async (event, callback) => {
//2. Store contact email in variable
var email = event.inputFields['email'];
//3. Configure and Make our HTTP request to Kickbox Email Verification API
@jackcoldrick90
jackcoldrick90 / associateContactToCustomObject.js
Created November 21, 2023 16:50
Associates a contact to a custom object. Using Axios library for HTTP requests.
const axios = require('axios');
axios.defaults.headers.common['authorization'] = `Bearer ${process.env.HUBSPOTTOKEN}`;
axios.defaults.headers.common['Content-Type'] = `application/json`;
axios.defaults.headers.put['Content-Type'] = `application/json`;
exports.main = async (event) => {
const partner_id = event.inputFields['partner_id'];
const partnerObjectId = await getPartner(partner_id);
@jackcoldrick90
jackcoldrick90 / associateCustomObjectToContact.js
Created August 10, 2023 21:59
Code will associate a custom object to the contact associated to a deal.
// Import HubSpot Client Library and instantiate client with private app token
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({
accessToken: process.env.HUBSPOTTOKEN // Access token associated with your private app (also be sure to include token in secrets)
});
// Helper function to get the associated Object ID of a specified Object Type
async function getAssociatedObjectId(objectType, objectId, toObjectType) {
let obj = await hubspotClient.crm.objects.associationsApi.getAll(objectType, objectId, toObjectType);
return obj.results[0].id;
@jackcoldrick90
jackcoldrick90 / splitFullName.js
Last active June 13, 2024 13:21
Using Hubspot Operations Hub Custom Coded workflow action you can split the full name provided by a contact and store in the firstname and lastname properties. Ideal if you're asking for the entire name in a single field on the form.
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["full_name"])
.then(results => {
@jackcoldrick90
jackcoldrick90 / workflow-1
Created July 25, 2023 15:19
Code snippets to be used in two separate Hubspot workflows to create a referral program. The first will generate a random number (referral ID) the second will be used to attribute referrals back to the referring contact.
//1. Import required libaries
const Promise = require("bluebird"); // Javascript Promise Library
const randomNumber = require("random-number-csprng"); // Javascript Random Number Generator
exports.main = (event, callback) => {
//2. Using a Promise, we first try to generate a random number
Promise.try(function() {
return randomNumber(10000, 50000); // Generate Random Number between a defined range
}).then(function(number) { // When the number is successfully generated we do this
@jackcoldrick90
jackcoldrick90 / searchMySQLDB.js
Created May 19, 2021 12:13
This is an example of a code snippet that checks to see if a contact exists in an external DB (MySQL). If it does we update a property to "true" otherwise "false".
// Import required libraries
const hubspot = require('@hubspot/api-client');
const mysql = require('mysql');
// This function is called when the custom coded action is executed
exports.main = (event, callback) => {
// Create a new instance of the HubSpot API Client - we need this to interact with the CRM APIs to retrieve information relating to the currently enrolled record
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
@jackcoldrick90
jackcoldrick90 / associateCompanyToContact.js
Created July 26, 2023 12:32
Script below can be used in a HubSpot Contact Workflow to associate a contact to a company using the value stored in the "company name" property. If the company exists it is associated. If the company does not exist, it is created and associated. Requires authentication with a private app.
// Import the Hubspot NodeJS Client Library - this will allow us to use the HubSpot APIs
const hubspot = require('@hubspot/api-client');
//Instantiate a new HubSpot API client using an access token (private app)
const hubspotClient = new hubspot.Client({
accessToken: process.env.HUBSPOTTOKEN
});
// Helper function to associate Company to Contact using HubSpot Client
function associateCompanyToContact(companyId, contactId) {
@jackcoldrick90
jackcoldrick90 / updateReferringContactHubspot.js
Created April 26, 2021 17:17
When a deal is created and the Referrer ID is known trigger a custom coded workflow action that will: Use the CRM Search API to Search for the contact with the matching Referrer ID. The CRM API to update the Contacts total number of referrals & Recent Referral Date properties.
// Import the required libraries - in this instance the HubSpot NodeJS API client
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
// Create a new instance of the HubSpot client
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
@jackcoldrick90
jackcoldrick90 / referralProgramAttribution-v2.js
Last active September 12, 2023 20:27
This script looks at the "referrer" property associated with the contact that has just converted and then leverages the CRM Search APi to find the matching contact with that specific "referrer ID". It then uses the CRM API to update the contacts "recent referral date" and "total referral" properties.
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
//1) Get the enrolled deal - referrer_id property value
hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["referrer"])