Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Last active April 30, 2023 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jackcoldrick90/36ff26fbf99e7514bec9966d5d5b9174 to your computer and use it in GitHub Desktop.
Save jackcoldrick90/36ff26fbf99e7514bec9966d5d5b9174 to your computer and use it in GitHub Desktop.
This snippet generates a unique code using Math.random(). This code can be used in a referral program
// Import the required libraries - HubSpot NodeJS API client
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
// Secrets can be accessed via 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)
.then(results => {
//Generate random referrer ID using Math.random() and store in variable
/*
IMPORTANT: There is a chance for a duplicate number to occur using Math.random(). this is just intended as an example.
Some ways to protect against this would be to use the CRM Search API to check if any other contact already has that ID and
if they do generate a new one. Alternatively you could look to create your own unique code using the current date in conjunction with Math.random()
*/
var referrerID = Math.floor(Math.random() * 1000);
callback({
outputFields: {
referrer_id: referrerID //Store the referrerID as a data output field
}
});
})
.catch(err => {
console.error(err);
});
}
@dshukertjr
Copy link

Hey @jackcoldrick90 !
Could the code look like this since the return value of the api call is not being used here?

exports.main = (event, callback) => {
    //Generate random referrer ID using Math.random() and store in variable
    /* 
      IMPORTANT: There is a chance for a duplicate number to occur using Math.random(). this is just intended as an example. 
      Some ways to protect against this would be to use the CRM Search API to check if any other contact already has that ID and 
      if they do generate a new one. Alternatively you could look to create your own unique code using the current date in conjunction with Math.random()
    */
    var referrerID = Math.floor(Math.random() * 1000);
    
    callback({
      outputFields: {
        referrer_id: referrerID //Store the referrerID as a data output field
      }
    });
}

@kvelland
Copy link

kvelland commented Apr 30, 2023

Hi @jackcoldrick90. I made a couple of suggestions here

  1. Updated to use the Private App in HubSpot
  2. Added a function to use not only digits but also characters, making it less chance for duplicate
const hubspot = require('@hubspot/api-client');

function generateRandomCode(length) {
  var result = '';
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  var charactersLength = characters.length;

  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }

  return result;
}

exports.main = (event, callback) => {
  
//Rembember to add a secret called "PrivateApp_AccessToken"
const hubspotClient = new hubspot.Client({ accessToken: process.env.PrivateApp_AccessToken });
  hubspotClient.crm.contacts.basicApi.getById(event.object.objectId)
    .then(results => {
    
var referrerID = generateRandomCode(6);
      
      callback({
        outputFields: {
          referrer_id: referrerID //Store the referrerID as a data output field
        }
      });
    })
    .catch(err => {
      console.error(err);
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment