⚙ Operations Hub

NGiron
Member

Custom coded Random List Splitting in Workflow

SOLVE

Hi everyone,

We're trying to list split the enrolled contacts randomly by percentage. So for example, I have 420,000 enrolled contacts in the Workflow and we wanted 10%, it'll give me 42,000 random contacts.

In this code, We're generating a random number then assign it to the property of the contact object

 

const hubspot = require('@hubspot/api-client');
var Promise = require("bluebird");
var randomNumber = require("random-number-csprng");

exports.main = (event, callback) => {
  callback(processEvent(event));
};

function processEvent(event) {
  const hubspotClient = new hubspot.Client({ apiKey: process.env.HAPIKEY });
  
  let contactId = event.object.objectId;
  
  hubspotClient.crm.contacts.basicApi
    .getById(contactId, ["email","phone"])
    .then(results => {
      let email = results.body.properties["email"];
  
  console.log("Email is " + email);

  //Get random number between 1 and 10
    
  Promise.try(function() {
    return randomNumber(1, 10);
  }).then(function(number) {
    console.log("Random number is ", number);
  
  // Write random number to contact property Random Number
  
  hubspotClient.crm.contacts.basicApi
    .update(
      contactId,
      {
        properties: {
          random_number: number
        }
      }
    )
  
  console.log("Your contact" + email +" was assigned random number", number);
  
  }).catch({code: "RandomGenerationError"}, function(err) {
      console.log("Something went wrong!");
  });

  });
}

 

 

Then let's say the generated number is the number 1, it'll add that to a static list then reset the "Random Number" property. Then it'll repeat the same flow for the next enrolled contact.

custom-code-workflow.png

 

The problem with this approach is that we don't know how many times it'll generate the number 1 so results will vary.

Any suggestion on how to better approach this in Workflow or should we resort to creating an app in Hubspot?

Our end goal is to be able to split the enrolled contacts then do more filtering so basically like querying the entire enrolled contacts but since a contact goes through the flow and hit all the steps, this might be hard to achieve in Workflow.

0 Upvotes
1 Accepted solution
KyleJepson
Solution
Inbound Professor
Inbound Professor

Custom coded Random List Splitting in Workflow

SOLVE

Are you wanting to be able to enroll your hypothetical 420,000 contacts multiple times and grab a different 10% each time? Or are you just sending them through once and using whatever 10% you get?

 

Rather than using random-number-csprng, you could use the remainder operator (%) on the object ID. Since you've already assigned the ID to the contactId variable, you could just do contactId % 10 to divide the contacts into 10 even groups. What that does is take the ID and divide it by 10 and give you the remainder, so you'll get a number from 0 (if it's a multiple to 10) to 9 (the highest possible remainder). (If you want it to be a number 1-10, then you could do (contactId % 10) + 1)

 

The upside of this approach is that you know you'll get 10 even groups. The only potential downside is that it isn't random, so every contact will get put into the same group every time. If you need contacts randomly re-assigned to groups, this won't work. But if you just need a group divided into 10 equal groups, this should do the trick.

View solution in original post

0 Upvotes
3 Replies 3
NGiron
Member

Custom coded Random List Splitting in Workflow

SOLVE

"[Are you wanting to be able to enroll your hypothetical 420,000 contacts multiple times and grab a different 10% each time?]"

Answer: Yes, not sure if we would need to enroll the 420,000 multiple times, but if we did, we just have to ensure there's not the same contacts in multiple lists. 

We want to split all enrolled contacts into 10 individual lists with no overlap between them. 

Example:
If I had 8 enrolled contacts: A, B, C, D, E, F, G, H

and I split it into 25% (4 lists): Result would be

List 1 = A, B
List 2 = C, D

List 3 = E, F

List 4 = G, H

So I think it sounds like your idea will work, but I'm just clarifying to make sure there wont be any contacts in multiple lists. 

0 Upvotes
KyleJepson
Solution
Inbound Professor
Inbound Professor

Custom coded Random List Splitting in Workflow

SOLVE

Are you wanting to be able to enroll your hypothetical 420,000 contacts multiple times and grab a different 10% each time? Or are you just sending them through once and using whatever 10% you get?

 

Rather than using random-number-csprng, you could use the remainder operator (%) on the object ID. Since you've already assigned the ID to the contactId variable, you could just do contactId % 10 to divide the contacts into 10 even groups. What that does is take the ID and divide it by 10 and give you the remainder, so you'll get a number from 0 (if it's a multiple to 10) to 9 (the highest possible remainder). (If you want it to be a number 1-10, then you could do (contactId % 10) + 1)

 

The upside of this approach is that you know you'll get 10 even groups. The only potential downside is that it isn't random, so every contact will get put into the same group every time. If you need contacts randomly re-assigned to groups, this won't work. But if you just need a group divided into 10 equal groups, this should do the trick.

0 Upvotes
sam_g
Participant

Custom coded Random List Splitting in Workflow

SOLVE

Hey @KyleJepson - one thing I'm noticing in my database is that 50% of the contact ids end in 1, I'm not sure why this is or if it is common across most HubSpot instances:

 

sam_g_0-1642705127390.png

 

Which unfortunately makes this method unreliable. If anyone else has any other thoughts / methods to do this that would be awesome.