# Unhandled Promise Rejection

**URL:** <https://community.hubspot.com/t/unhandled-promise-rejection/56094>\
**Category:** Data Hub\
**Created:** [May 3, 2022, 1:33pm UTC](https://community.hubspot.com/t/unhandled-promise-rejection/56094 "2022-05-03T13:33:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_ThinkFuel](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin_thinkfuel/32/18964_2.png) [@Kevin\_ThinkFuel](https://community.hubspot.com/u/Kevin_ThinkFuel)\
**Post date:** [May 3, 2022, 1:33pm UTC](https://community.hubspot.com/t/unhandled-promise-rejection/56094/1 "2022-05-03T13:33:54Z")

</div>

Hi there, we are trying to recreate the referral program described in [this post](https://www.linkedin.com/pulse/create-referral-program-hubspot-using-marketing-hub-jack-coldrick/). However, when we run the last custom code workflow to update the referral contact to attribute the new lead to them, we are receiving a “Unhandled Promise Rejection”.

The code we are using (referenced in the above article) can be [found here](https://gist.githubusercontent.com/jackcoldrick90/b38268080650f8531650c2b47f681d8d/raw/bdf6e44b185983a15c53d384349192aeff48ae8e/referralProgramAttribution-v3.js).

For reference, here is the full error we are seeing when we test the action.

`ERROR	Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"HttpError: HTTP request failed","reason":{"errorType":"HttpError","errorMessage":"HTTP request failed","response":{"statusCode":401,"body":{"status":"error","message":"Authentication credentials not found. This API supports both API Key and OAuth 2.0 authentication and you can find more details at https://developers.hubspot.com/docs/methods/auth/oauth-overview","correlationId":"30e6a1fa-a231-46ad-94aa-87f1eced4a86","category":"INVALID_AUTHENTICATION"},"headers":{"date":"Tue, 03 May 2022 13:45:47 GMT","content-type":"application/json;charset=utf-8","content-length":"316","connection":"close","cf-ray":"70597065a98059c1-IAD","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"Accept-Encoding","cf-cache-status":"DYNAMIC","access-control-allow-credentials":"false","expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"","x-hubspot-correlation-id":"30e6a1fa-a231-46ad-94aa-87f1eced4a86","x-trace":"2B32E2962B5C921E3D642851280492D0C678F1F61F000000000000000000","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=jLhRHUGmlC4kIAeoU21Og0G3ZnoU2OEhpwSHCrjZ1aPM%2Bg5xs3806jwZOIwVyAfV%2Fm8tDi2sDfqfET%2FCjaaGcvXKWN5mMk6U0A%2FMSOh%2BUT5Z6Vb1kuCWeiC%2F%2B9XN6Sdv\"}],\"group\":\"cf-nel\",\"max_age\":604800}","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"api.hubapi.com","port":443,"hostname":"api.hubapi.com","hash":null,"search":null,"query":null,"pathname":"/crm/v3/objects/contacts/search","path":"/crm/v3/objects/contacts/search","href":"https://api.hubapi.com/crm/v3/objects/contacts/search"},"method":"POST","headers":{"User-Agent":"hubspot-api-client-nodejs; 3.4.1","Accept":"application/json","content-type":"application/json","content-length":255}}},"body":{},"statusCode":401,"name":"HttpError","stack":["HttpError: HTTP request failed"," at Request._callback (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/api/searchApi.js:151:40)"," at Request.self.callback (/opt/nodejs/node_modules/request/request.js:185:22)"," at Request.emit (events.js:314:20)"," at Request.<anonymous> (/opt/nodejs/node_modules/request/request.js:1154:10)"," at Request.emit (events.js:314:20)"," at IncomingMessage.<anonymous> (/opt/nodejs/node_modules/request/request.js:1076:12)"," at Object.onceWrapper (events.js:420:28)"," at IncomingMessage.emit (events.js:326:22)"," at endReadableNT (_stream_readable.js:1241:12)"," at processTicksAndRejections (internal/process/task_queues.js:84:21)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: HttpError: HTTP request failed"," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:314:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}`

---

<div class="post-metadata">

**Author:** ![Kevin\_ThinkFuel](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin_thinkfuel/32/18964_2.png) [@Kevin\_ThinkFuel](https://community.hubspot.com/u/Kevin_ThinkFuel)\
**Post date:** [May 3, 2022, 1:36pm UTC](https://community.hubspot.com/t/unhandled-promise-rejection/56094/2 "2022-05-03T13:36:30Z")

</div>

Removed.

---

<div class="post-metadata">

**Author:** ![M\_Mott](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@M\_Mott](https://community.hubspot.com/u/M_Mott)\
**Post date:** [May 25, 2022, 8:37pm UTC](https://community.hubspot.com/t/unhandled-promise-rejection/56094/3 "2022-05-25T20:37:16Z")

</div>

Hi,

Did you find a solution for this? I’m receiving the same error when I try to recreate that referral program.

Thanks!

---

<div class="post-metadata">

**Author:** ![Kevin\_ThinkFuel](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin_thinkfuel/32/18964_2.png) [@Kevin\_ThinkFuel](https://community.hubspot.com/u/Kevin_ThinkFuel)\
**Post date:** [May 26, 2022, 2:56pm UTC](https://community.hubspot.com/t/unhandled-promise-rejection/56094/4 "2022-05-26T14:56:58Z")

</div>

We did get it figured out. Here’s the code we ended up using.

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) => {
 const hubspotClient = new hubspot.Client({
 apiKey: process.env.HAPIKEY
 });

 const referrer = event.inputFields['referrer'];
 console.log("REFERRED ID: " + referrer);

 //2) Search for the contact using the deals referrer_id and update
 const filter = {
 propertyName: 'referrer_id',
 operator: 'EQ',
 value: referrer
 }
 const filterGroup = {
 filters: [filter]
 }
 const sort = JSON.stringify({
 propertyName: 'referrer_id',
 direction: 'DESCENDING'
 })
 const properties = ['referrer_id', 'total_referrals', 'firstname', 'lastname', 'email']
 const limit = 100
 const after = 0

 const publicObjectSearchRequest = {
 filterGroups: [filterGroup],
 sorts: [sort],
 properties,
 limit,
 after,
 }

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

 let contactId = results.body.results[0].id;
 let referrer_email = results.body.results[0].properties.email
 let totalReferrals = 0;

 if (results.body.results[0].properties.total_referrals === null || results.body.results[0].properties.total_referrals === undefined || results.body.results[0].properties.total_referrals === "") {
 totalReferrals = 0;
 } else {
 totalReferrals = parseInt(results.body.results[0].properties.total_referrals)
 }

 let totalReferralsUpdated = totalReferrals + 1;

 var d = new Date();
 d.setUTCHours(0, 0, 0, 0);

 	//Update Referred Contact 	
 hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
 "properties": {
 "referred_by": referrer_email
 }
 })

		//Update Referring Contact 	
 hubspotClient.crm.contacts.basicApi.update(contactId, {
 "properties": {
 "total_referrals": totalReferralsUpdated,
 "recent_referral_date": d
 }
 })
 });
}

```

---

<div class="post-metadata">

**Author:** ![M\_Mott](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@M\_Mott](https://community.hubspot.com/u/M_Mott)\
**Post date:** [May 26, 2022, 5:39pm UTC](https://community.hubspot.com/t/unhandled-promise-rejection/56094/5 "2022-05-26T17:39:01Z")

</div>

Thank you!
