Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
jackcoldrick90 / createTrelloBoardListAndCard.js
Created June 9, 2021 08:36
Using Trello Rest API we can update boards/lists/cards when data changes within the HubSpot CRM. In the below example we create a new board, create a list within that board and add a test card to that board. https://developer.atlassian.com/cloud/trello/rest/api-group-actions/
const hubspot = require('@hubspot/api-client');
const request = require('request');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
hubspotClient.crm.deals.basicApi.getById(event.object.objectId, ["dealname"])
@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 / 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 / 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'];
@jackcoldrick90
jackcoldrick90 / addDateSuffix.js
Last active March 8, 2022 12:50
This script is designed to be used within a custom coded workflow action (Operations Hub Professional) and adds the appropriate suffix depending on the day of the month. It relies on you already having the date stored in a single line text property DD/MM/YYYY. Worth noting this assumes you are storing the date in DD/MM/YYYY format. If you are us…
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, ["selected_date"])
.then(results => {
@jackcoldrick90
jackcoldrick90 / createContactfromConversationThread.js
Created April 8, 2022 14:07
Using the Conversations Inbox & Messaging API (https://developers.hubspot.com/docs/api/conversations/conversations) we can pull the body of an email, parse the content and create a contact in the CRM using the Contact API (https://developers.hubspot.com/docs/api/crm/contacts).
//1. Import required libraries
const hubspot = require('@hubspot/api-client');
const axios = require('axios');
exports.main = async (event, callback) => {
//2. Store threadID in variable, we'll use this to pull information
const threadID = event.inputFields['hs_thread_id'];
//3. Make a request to the conversations API to retrieve text - https://developers.hubspot.com/docs/api/conversations/conversations