Custom code not working - reduce object ID to a single digit

Hi there,

I’m a marketing ops person with no code experience. We have a workflow with a custom code to send marketing emails from HubSpot over to Planhat (that part is ok, code provided by old partner). The idea for this is that our support agents can check the latest mkt communications received in PlanHat & dn’t have to go to HubSpot all the time.

However, since most our customers receive our newsletters at the same time, they all enroll in the workflow at the same time & the custom code throws the error of too many API calls at once -->

Error while retrieving email: You have reached your ten_secondly_rolling limit.

I thought it’d be a good idea to use the custom code used in the “Workflows Deep Dive” class from HubSpot, where there is a part to create a custom coded action for a weighted lead rotation.

The idea is that people who enroll in the workflow will then be split up in 5 different groups, I’d then send them down 5 different branches with the same custom code from Planhat but with different delays; hoping not to encounter the “ten_secondly_rolling limit” error.

So looking at the code from the HS training, gave me something like this:

exports.main = async (event, callback) => {
	return callback({
 outputFields:{
 test: event.object.hs_object_id % 5
 }
 });

I have defined the data type & name of the output (number & test) in the output fields; but when I test this piece of custom code; I get an error that it’s not defined in the code.


Custom code action - Workflows Deep Dive example

Not defined in code error?

I then asked ChatGpt “can you write me a Node.js 16.x custom coded script for HubSpot Client v8 that divides the number in ‘hs_object_id’ until it’s a number less than 5 ? I want the result to be in an output called ‘test’. This is for a custom coded workflow in HubSpot”

& I got this code

exports.main = async (event, callback) => {
 try {
 const hsObjectId = event.object.hs_object_id;
 let result = hsObjectId;

 while (result >= 5) {
 result /= 5;
 }

 const output = {
 test: result,
 };

 return callback({
 outputFields: output,
 });
 } catch (error) {
 return callback({
 outputFields: {
 error: error.message,
 },
 });
 }
};

Unfortunately, this gave me the same error as in the example from the HubSpot training.

Can anyone please help me figure out why it doesn’t work?

Hey Hanne! Already wrote you in Slack, but wanted to add here as well in case other people have the same request. In essence the code will use the RecordId within HubSpot and utilize the last number to break the contacts down into 5 groups and save that assigned group into a property.

I’ve added comments to the code for easier digestion!

exports.main = (event, callback) => {

 // Storing the RecordID into a const
 const recordid = event.inputFields['recordid'];

 // Convert the const to a string
 const recordidStr = String(recordid);

 // Cutting to get the last digit
 const recordlastDigit = recordidStr.slice(-1);

 // Convert the last digit back to a number
 const lastDigitNumber = parseInt(recordlastDigit);

 // Just some console logs for testing purposes
 console.log(`The original recordId is: ${recordid}`);
 console.log(`The last digit of recordid is: ${lastDigitNumber}`);

 // Creating the ranges for the 5 groups via a switch statement
 let sendGroup;

 switch (true) {
 case lastDigitNumber >= 0 && lastDigitNumber <= 1:
 sendGroup = 1;
 break;
 case lastDigitNumber >= 2 && lastDigitNumber <= 3:
 sendGroup = 2;
 break;
 case lastDigitNumber >= 4 && lastDigitNumber <= 5:
 sendGroup = 3;
 break;
 case lastDigitNumber >= 6 && lastDigitNumber <= 7:
 sendGroup = 4;
 break;
 case lastDigitNumber >= 8 && lastDigitNumber <= 9:
 sendGroup = 5;
 break;
 default:
 sendGroup = 0; // or any other value you want
}

 console.log(`The group assigned is: ${sendGroup}`);

 // Stores the send group in a property to be used in workflows etc.
 callback({
 outputFields: {
 sendgroup: sendGroup,
 },
 });
};

Hi @BenLerner-Paio

Thanks a lot for your help! The code is super clear thanks to your comments in it.
I implemented it, and your code is working perfectly. Hope this can help others too :blush: