# Custom coded workflow gives errors with bulk enrollment

**URL:** <https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442>\
**Category:** Data Hub\
**Created:** [February 3, 2023, 3:13pm UTC](https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442 "2023-02-03T15:13:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DouweDeinum](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@DouweDeinum](https://community.hubspot.com/u/DouweDeinum)\
**Post date:** [February 3, 2023, 3:13pm UTC](https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442/1 "2023-02-03T15:13:41Z")

</div>

Hi,  
we prepared a workflow with a custom coded action. When we test the custom code on individual enrolled records, it works perfectly.  
But when I turn on the workflow and enroll all records that need to go through the custom code, most of them are skipped because of an error. And the error is very unclear. It shows only:

 ![DouweDeinum_0-1675437090879.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/b/9/b92e82a969759244b3c4ca9c64b8912d9718a5f3.png)

We have enough API calls to work with, and HubSpot does not say that we hit the limits. So that should not be the case.  
Digging deeper does not clarify what the problem is. Has anyone some ideas what could cause the issue? Has anyone a similar experience, an if yes how did yo resolve that?

---

<div class="post-metadata">

**Author:** ![MiaSrebrnjak](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/miasrebrnjak/32/10_2.png) [@MiaSrebrnjak](https://community.hubspot.com/u/MiaSrebrnjak)\
**Post date:** [February 6, 2023, 1:43pm UTC](https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442/2 "2023-02-06T13:43:00Z")

</div>

Hi @DouweDeinum,

Thank you for reaching out to the Community!

Could you share the custom code you’re using? Or at least a part of it?

I’ll tag in a couple of subject matter experts though to see if they have any insight on this:

Hi @miljkovicmisa, @louischausse, @Teun, have you ever encountered such an error? Thanks!

Cheers  
Mia, Community Team

---

<div class="post-metadata">

**Author:** ![DouweDeinum](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@DouweDeinum](https://community.hubspot.com/u/DouweDeinum)\
**Post date:** [February 7, 2023, 7:59am UTC](https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442/3 "2023-02-07T07:59:16Z")

</div>

Hi,

see custom code listed here. The goal is to set an association between company and deal based on a property in the deal. This property is always filled.

// Get the HubSpot client  
const hubspot = require(‘@hubspot/api-client’);  
const http = require(“https”);  
const {execSync} = require(‘child\_process’);

// Call main function  
exports.main = (event, callback) =\> {  
return callback(processEvent(event));};

// Process the work  
function processEvent(event) {

// HAPIKEY is defined in secrets at the Workflow / Custom Code  
// Instantiate the HS Client

const hubspotClient = new hubspot.Client({  
accessToken: process.env.WorkflowCustomCode  
});

// Interval  
const intMin = 2;  
const intMax = 4;

// Get the Deal ID  
const dealId = event.object.objectId;

// Declare constants  
const fromObjectType = “Deal”;  
const toObjectType = “companies”;  
const associationType = “deal\_to\_company”;

// Declare variables  
let companyId = 0

// Get the my\_store\_id property value  
// It looks like we need to use the searchApi instead of the basicApi as the custom properties are not returned by the basicApi

const dealProperties = [“order\_store\_gln”]  
const dealFilter = { propertyName: “hs\_object\_id”, operator: “EQ”, value: dealId }  
let dealFilterGroup = { filters: [dealFilter] }  
let dealSearchRequest = { properties: dealProperties, filterGroups: [dealFilterGroup] }  
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));  
hubspotClient.crm.deals.searchApi.doSearch(dealSearchRequest)  
.then (dealResults =\> {  
if (dealResults.body.total \> 0) {

// We have found the deal  
// Let’s get the company based on order\_store\_gln  
let storeId = dealResults.body.results[0].properties.order\_store\_gln;  
if (storeId \> 0) {  
const companyFilter = { propertyName: “storegln”, operator: “EQ”, value: storeId }  
const companyFilterGroup = { filters: [companyFilter] }  
const companySearchRequest = { filterGroups: [companyFilterGroup] }  
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));  
hubspotClient.crm.companies.searchApi.doSearch(companySearchRequest)  
.then (companyResult =\> {  
if (companyResult.body.total \> 0) {  
companyId = companyResult.body.results[0].id  
if (companyId \> 0) {

// We have found the company to associate with  
// Let’s check if there already is an association with another company; that one should be deleted.

const BatchInputPublicObjectId = { inputs: [{“id”:dealId}] };  
try {  
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));  
hubspotClient.crm.associations.batchApi.read(fromObjectType, toObjectType, BatchInputPublicObjectId)  
.then (assResults =\> {  
if (assResults.body) {  
for (let i = 0; i \< assResults.body.results.length; i++) {  
let assCompanyId = assResults.body.results[i].to[0].id  
if (assCompanyId != companyId) {

// Delete the current association  
var options = {  
“method”: “DELETE”,  
“hostname”: “[api.hubapi.com](http://api.hubapi.com)”,  
“port”: null,  
“path”: “/crm/v4/objects/Deal/” + dealId + “/associations/Company/” + assCompanyId + “?hapikey=” + process.env.HAPIKEY,  
“headers”: {  
“accept”: “application/json”  
}  
};  
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));  
var req = http.request(options);  
req.end();  
}  
}  
}  
})

// We can now create the new association  
try {  
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));  
hubspotClient.crm.deals.associationsApi.create(dealId, toObjectType, companyId, associationType)  
} catch (e) {  
e.message === ‘HTTP request failed’  
? console.error(JSON.stringify(e.response, null, 2))  
: console.error(e)  
}  
} catch (e) {  
e.message === ‘HTTP request failed’  
? console.error(JSON.stringify(e.response, null, 2))  
: console.error(e)  
}  
}  
}  
})  
}  
}  
})  
}

function randomIntFromInterval(min, max) { // min and max included  
return Math.floor(Math.random() \* (max - min + 1) + min)  
}

---

<div class="post-metadata">

**Author:** ![DouweDeinum](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@DouweDeinum](https://community.hubspot.com/u/DouweDeinum)\
**Post date:** [February 7, 2023, 9:04am UTC](https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442/4 "2023-02-07T09:04:42Z")

</div>

We found still a HAPIKEY instruction. Apparently we missed that one. So that is at least an error fixed 😮

---

<div class="post-metadata">

**Author:** ![DouweDeinum](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@DouweDeinum](https://community.hubspot.com/u/DouweDeinum)\
**Post date:** [February 7, 2023, 9:45am UTC](https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442/5 "2023-02-07T09:45:18Z")

</div>

We found still a HAPIKEY instruction. Apparently we missed that one. So that is at least an error fixed 😮; And we finally got a hint that points towards the secondly limit.

Thanks for your response, I think we now know what to do.

---

<div class="post-metadata">

**Author:** ![louischausse](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/louischausse/32/18067_2.png) [@louischausse](https://community.hubspot.com/u/louischausse)\
**Post date:** [February 6, 2023, 1:55pm UTC](https://community.hubspot.com/t/custom-coded-workflow-gives-errors-with-bulk-enrollment/74442/6 "2023-02-06T13:55:04Z")

</div>

Can’t know for sure without seeing the code and the records you are enrolling but it seems to me that maybe some of the records in the bulk enroll don’t have known value for all the required properties set as variable in your code.  
This is why it works with individual test record but when you enroll them all it fails for some.  
Don’t forget to mark my reply as a solution if you are satisfied. If not, do not hesitate to ask me anything!
