# Error is custom code in Automation

**URL:** <https://community.hubspot.com/t/error-is-custom-code-in-automation/110002>\
**Category:** APIs & Integrations\
**Tags:** professional\
**Created:** [June 24, 2024, 10:55pm UTC](https://community.hubspot.com/t/error-is-custom-code-in-automation/110002 "2024-06-24T22:55:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TMiller4](https://avatars.discourse-cdn.com/v4/letter/t/cab0a1/32.png) [@TMiller4](https://community.hubspot.com/u/TMiller4)\
**Post date:** [June 24, 2024, 10:55pm UTC](https://community.hubspot.com/t/error-is-custom-code-in-automation/110002/1 "2024-06-24T22:55:04Z")

</div>

HI,

It has been awhile since I have used custom code in Hubspot, back when it was API vs. Private Apps. I am tyring to use the following code to populate data into Hubspot for a referred contact. I think my error is in how I am trying to use the Private App Toekn. Any help is greaterly appreciated.

I undestand I can’t post certain words in the post, I assume this word is not allow in context of geenrating busniness. I changed the format of it to refer(ral), as this code is about a refe(ral) propgram I saw in the Hubspot Community.

The post also automatically removed invalid HTML from the error message from the log posted below, so it may read different.

The code is pasted below and the error from the logs below it.

**Code:**

/\*

Operations Hub Workshop 3: Workflow 2 Code

This code is used to attribute a refer(ral) back to the correct contact. This is done by querying the CRM to find a contact matching the referrer value associated to the enrolled contact. We then update the appropriate properties on both the referring and referred contact.

\*/

//1. Import required libraries

const hubspot = require(‘@hubspot/api-client’); // HubSpot Node Client will allow us to make calls to HubSpot API

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

//2. Create our client

const hubspotClient = new hubspot.Client({

accessToken: process.env.HAPIKEY // Reference our Private App Token - this should have been added as a secret

});

//3. Store the referrer ID linked to the enrolled contact in a variable

const referrer = event.inputFields[‘referrer’];

//4. Configure our search query

const filterGroup = {

filters: [{ // filters are used to limit the results returned. We will simply filter for any contact who has a matching value for the referrer ID

propertyName: ‘referrer\_id’,

operator: ‘EQ’,

value: referrer

}]

}

// We specify what properties we’d like to return from this search

const properties = [‘referrer\_id’, ‘number\_of\_refer(ral)s’, ‘firstname’, ‘lastname’, ‘email’]

// We create our search object which we will pass to our api call

const publicObjectSearchRequest = {

filterGroups: [filterGroup],

properties

}

//5. Search the CRM for the contact using the query defined above

hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest).then(results =\> {

//6. Store data in variables

let contactId = results.body.results[0].id; // Id of the referring contact

let referred\_by = results.body.results[0].properties.firstname + " " + results.body.results[0].properties.lastname; // full name of the referring contact

let totalReferrals = 0;

console.log(“referred\_by” + referred\_by);

//7. Check to see if this is their first refer(ral). If it is we set “totalRefer(rals)” property to “0”

if (results.body.results[0].properties.number\_of\_refer(ral)s === null || results.body.results[0].properties.number\_of\_refer(ral)s === undefined || results.body.results[0].properties.number\_of\_refer(ral)s === “”) {

totalRefer(ral)s = 0;

} else { // Otherwise we retrieve the current number of refer(ral)s

totalRefer(ral)s = parseInt(results.body.results[0].properties.number\_of\_refer(ral)s)

}

let totalReferralsUpdated = totalRefer(ral)s + 1; // Increment refer(ral)s

//8. Create a new date (todays date)

var d = new Date();

d.setUTCHours(0, 0, 0, 0);

//9. Update Referred Contact’s “referred by” property

hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {

“properties”: {

“referred\_by”: referred\_by

}

})

//10. Update Referring Contacts “number of refer(ral)s” and “recent refer(ral) date” properties

hubspotClient.crm.contacts.basicApi.update(contactId, {

“properties”: {

“number\_of\_refer(ral)s”: totalRefer(ral)sUpdated,

“recent\_refer(ral)\_date”: d

}

})

});

}

**Error:**

```auto
WARNING: The logs for this function have exceeded the 4KB limit.
...
,\"correlationId\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"898f8b4a695d0584-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Mon, 24 Jun 2024 20:42:23 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=%2BsTIoc08tLHRT%2FOnsRv3aNpTFJSKK7OZ5j0VDMmaKhYJjOgeAx%2F1cnREWjrEZ1UtMfO%2FemV6gaQsP9Xse978urSQYGSrjVMuJ9DhUwOVIO%2Bx7z4Wlekk4rb0%2Fgl6yq4k\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"70\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-7dd59b876-zb9w4\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499998\",\"x-request-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}"," at SearchApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:59:23)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:5:58)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 400","Message: An error occurred.","Body: {\"status\":\"error\",\"message\":\"There was a problem with the request.\",\"correlationId\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"898f8b4a695d0584-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Mon, 24 Jun 2024 20:42:23 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=%2BsTIoc08tLHRT%2FOnsRv3aNpTFJSKK7OZ5j0VDMmaKhYJjOgeAx%2F1cnREWjrEZ1UtMfO%2FemV6gaQsP9Xse978urSQYGSrjVMuJ9DhUwOVIO%2Bx7z4Wlekk4rb0%2Fgl6yq4k\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"70\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-7dd59b876-zb9w4\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499998\",\"x-request-id\":\"50501b8e-c256-4242-820d-116b493ff4a3\"}"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:519:28)"," at emit (node:internal/process/promises:150:20)"," at processPromiseRejections (node:internal/process/promises:284:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}
Unknown application error occurred
Runtime.Unknown

Memory: 21/128 MB
Runtime: 2254.24 ms

```

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [June 25, 2024, 7:14am UTC](https://community.hubspot.com/t/error-is-custom-code-in-automation/110002/2 "2024-06-25T07:14:52Z")

</div>

Hi @TMiller4

Problem with your search API payload, see the screenshot below:  
In the screenshot, Please replace properties with properties:properties.

 ![GRajput_0-1719299650159.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/8/6/86a969f6ebc5921254023e74214a37da12419f95.png)

I hope this will help you out. Please mark it as **Solution Accepted and upvote** to help another Community member.  
Thanks!

---

<div class="post-metadata">

**Author:** ![TMiller4](https://avatars.discourse-cdn.com/v4/letter/t/cab0a1/32.png) [@TMiller4](https://community.hubspot.com/u/TMiller4)\
**Post date:** [June 25, 2024, 8:44pm UTC](https://community.hubspot.com/t/error-is-custom-code-in-automation/110002/3 "2024-06-25T20:44:38Z")

</div>

Hi,

Thanks for your fast response and assistance. I made the change, unfortunately it is still throwing an error and not populating the data back into Hubspot.

Did I used the Private App/Token correctly where the Secret “HAPIKey” is the Token from the private app.:

"//2. _Create our client_  
const hubspotClient = new hubspot.Client({  
accessToken: process.env.HAPIKEY // _Reference our Private App Token - this should have been added as a secret_

I see a yellow highlight with a message for line of code: exports.main = (event, callback) =\> { with the message ‘Callback’ is defined but never used.

Here is the verbiage from the Logs:

```auto
WARNING: The logs for this function have exceeded the 4KB limit.
...
2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"85\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at SearchApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:59:23)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:5:58)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 400","Message: An error occurred.","Body: {\"status\":\"error\",\"message\":\"There was a problem with the request.\",\"correlationId\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"85\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:519:28)"," at emit (node:internal/process/promises:150:20)"," at processPromiseRejections (node:internal/process/promises:284:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}
Unknown application error occurred
Runtime.Unknown

Memory: 86/128 MB

```

```auto
WARNING: The logs for this function have exceeded the 4KB limit.
...
2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"85\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at SearchApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:59:23)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/apis/SearchApi.js:5:58)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: HTTP-Code: 400","Message: An error occurred.","Body: {\"status\":\"error\",\"message\":\"There was a problem with the request.\",\"correlationId\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}","Headers: {\"access-control-allow-credentials\":\"false\",\"cf-cache-status\":\"DYNAMIC\",\"cf-ray\":\"8997aed16e9757be-IAD\",\"connection\":\"keep-alive\",\"content-length\":\"123\",\"content-type\":\"application/json;charset=utf-8\",\"date\":\"Tue, 25 Jun 2024 20:24:45 GMT\",\"nel\":\"{\\\"success_fraction\\\":0.01,\\\"report_to\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"report-to\":\"{\\\"endpoints\\\":[{\\\"url\\\":\\\"https:\\\\/\\\\/a.nel.cloudflare.com\\\\/report\\\\/v4?s=dlwUzWMg%2BEgHWkGwCgBXG%2BsZr9bfWgU3BxWUs%2Fg1WQHaBHkCpaswB%2B32YAwXfJq%2F8YELdAfog%2By2kS3G4toOlN%2BQcgfLrjA9wZrAtUjxS6LrUYYEMW9vYIZVHKms%2B1zp\\\"}],\\\"group\\\":\\\"cf-nel\\\",\\\"max_age\\\":604800}\",\"server\":\"cloudflare\",\"strict-transport-security\":\"max-age=31536000; includeSubDomains; preload\",\"vary\":\"origin, Accept-Encoding\",\"x-content-type-options\":\"nosniff\",\"x-envoy-upstream-service-time\":\"85\",\"x-evy-trace-listener\":\"listener_https\",\"x-evy-trace-route-configuration\":\"listener_https/all\",\"x-evy-trace-route-service-name\":\"envoyset-translator\",\"x-evy-trace-served-by-pod\":\"iad02/hubapi-td/envoy-proxy-7dd59b876-smxcv\",\"x-evy-trace-virtual-host\":\"all\",\"x-hubspot-correlation-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\",\"x-hubspot-ratelimit-daily\":\"500000\",\"x-hubspot-ratelimit-daily-remaining\":\"499999\",\"x-request-id\":\"f8e0e34a-62e2-4631-8e1c-d1b75bdc5730\"}"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:519:28)"," at emit (node:internal/process/promises:150:20)"," at processPromiseRejections (node:internal/process/promises:284:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}
Unknown application error occurred
Runtime.Unknown

Memory: 86/128 MB
Runtime: 2181.08 ms

```

---

<div class="post-metadata">

**Author:** ![TMiller4](https://avatars.discourse-cdn.com/v4/letter/t/cab0a1/32.png) [@TMiller4](https://community.hubspot.com/u/TMiller4)\
**Post date:** [July 10, 2024, 5:11pm UTC](https://community.hubspot.com/t/error-is-custom-code-in-automation/110002/4 "2024-07-10T17:11:54Z")

</div>

Hi,

I solved the issues I was having with the code from Jack Coldrick’s Creating a Refewrral Session in Hubspot. There were three issues resolved by changes to the coe originally provided in the session.

1. The solution above changing “properties:” to “properties:properties”.

2. Removing “.body” from the the orginal code as it is no loinger required (found in a different post)

3. Being sure the property internal names I had created matched the ones I was using in the code.
