Large enrollment Workflows with custom code

Hi Everyone,
I know this post is a bit older now, but I thought I’d add a solution for anyone that’s having similar issues.
The issue with using any HubSpot delays is that if all the objects are enrolled at the same time, for example when an import is done they will hit the delay stage at the same time, therefore they will all still hit your custom coded step at the same time, not resolving the limit issue.
What I suggest doing is an exponentially increasing randomised delay using setTimeout(). I’ve put an example of how this can be done below:

{

 let timeout = 5000;
 const maxTries = 5;
 let outcome = 0;
 let data;

 for (let i = 1; i <= maxTries; i++) {
 const response = await fetch(url, options);
 if (response.status === 200) {
 data = await response.json();
 outcome = 200;
 break;
 } else {
 console.log(`Attempt ${i} failed. Retrying in ${timeout}ms...`);
 await new Promise(resolve => setTimeout(resolve, timeout));
 timeout *= 3*Math.random();
 }
 }

 if (outcome !== 200) {
 throw new Error(`Max retries reached. Unable to fetch data.`);
 }

}

As you can see, everytime the response fails, it multiples the delay anywhere from 0-3 times and retries up to 5 times before erroring.
For the timing I would suggest finding out what you’re largest import/creation event would be and see what timeframe this would need to be split over to be within the rate limit.