Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
jackcoldrick90 / associateContactToCompanyUsingID.js
Created July 27, 2021 14:06
Using an ID supplied by a contact on a form we can trigger custom coded automation to find a company in the CRM matching that ID and associate accordingly. This uses the CRM Contacts API to retrieve the ID provided by the contact. The CRM Search API to find the matching company (if any) and the CRM Associations API to make the association betwee…
// Import the Hubspot NodeJS Client Library - this will allow us to use the HubSpot APIs
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
// Instantiate a new HubSpot API client using the HAPI key (secret)
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
@jackcoldrick90
jackcoldrick90 / generateRandomNumber.js
Created August 18, 2021 15:22
Using HubSpot Operations Hub and the csprng library we can generate a random number.
const hubspot = require('@hubspot/api-client');
var Promise = require("bluebird");
var randomNumber = require("random-number-csprng");
exports.main = (event, callback) => {
Promise.try(function() {
return randomNumber(1, 2);
@jackcoldrick90
jackcoldrick90 / formatPhoneNumber.js
Created October 1, 2021 13:45
Using custom coded workflow actions you are able to format a phone number using a regular expression.
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
// Secrets can be accessed with environment variables.
// Make sure to add your API key under "Secrets" above.
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["phone"])
@jackcoldrick90
jackcoldrick90 / createAdditionalDeals.js
Created October 14, 2021 14:27
This code snippet is ideal if you want to create additional deals based on a "contract length" for reporting purposes. It's important to ensure you have created a deal property "Contract Length" as this is what is responsible for enrolling deals into the workflow and also for ensuring we create the right amount of deals. You are free to modify a…
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
// Setup the HubSpot API Client
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
// Use the client to pull information relating to the currently enrolled deal
@jackcoldrick90
jackcoldrick90 / connectAndQueryMySQLDatabase.js
Created November 15, 2021 15:00
Operations Hub Professional - Custom Coded Workflow action that can be used to connect to and query a MySQL database. Important to note that you must define your secrets within your own workflow. They would store the database user name, password, hostname and table name. In this example we connect to the database, which is hosted on AWS and sear…
// Include the mysql node library
const mysql = require('mysql');
// Function is called when custom code action is executed
exports.main = async (event, callback) => {
// Define variables using CRM data as an input
const email = event.inputFields['email'];
const firstName = event.inputFields['firstname'];
const lastName = event.inputFields['lastname'];
@jackcoldrick90
jackcoldrick90 / operations-hub-workshop.sql
Created November 15, 2021 19:09
Script can be used to create a test mySQL database to be queried using a custom coded workflow action
/* create database "customers" */
CREATE DATABASE customers;
/* use the database "customers" */
USE customers;
/*
Create table in the "customers" database called "customer_info".
This will have columns to hold data relating to our customers namely
their name and email address. CustomerID is the primary key and is an auto-generaged
@jackcoldrick90
jackcoldrick90 / operations_hub_workshop_1_example_1.js
Last active March 15, 2023 19:02
OPERATIONS HUB WORKSHOP #1: Data Enrichment using Clearbit - Collection of code snippets from the first workshop.
const request = require('request');
exports.main = async (event, callback) => {
//1. Store the company domain in a variable
var companyDomain = event.inputFields['domain'];
var domainAlias, tech, subIndustry, legalName;
//2. Configure request to Clearbit Discovery API
var options = {
@jackcoldrick90
jackcoldrick90 / operations_hub_workshop_2_example_1.js
Created February 10, 2022 10:41
OPERATIONS HUB WORKSHOP #2: Email Validation using Kickbox - Collection of code snippets from the first workshop.
/* Example 1: Make a HTTP Request to Kickbox Verification API using NodeJS request. */
//1. Import libraries, in this instance "request" to make HTTP requests
const request = require('request')
exports.main = async (event, callback) => {
//2. Define variables to store data
var role, free, disposable, sendex, result, reason; // We will use these later on when data is returned from Kickbox
const email = event.inputFields['email']; // We store the contact email in a variable and will query Kickbox
@jackcoldrick90
jackcoldrick90 / plus1referral.js
Created February 16, 2022 12:37
This code will create a contact off the back of a form submission whereby a user is prompted to provide details for a plus 1.
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const fname = event.inputFields['firstname'];
const lname = event.inputFields['lastname'];
const plus1FirstName = event.inputFields['plus_1_first_name'];
const plus1LastName = event.inputFields['plus_1_last_name'];
const plus1EmailAddress = event.inputFields['plus_1_email_address'];
/*
Operations Hub Workshop #3: Workflow 1 Code
This code is used to generate a random number that will be assigned to anyone who joins our referral program.
*/
//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) => {