<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Unhandled Promise Rejection in Data Hub</title>
    <link>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/639581#M875</link>
    <description>&lt;P&gt;We did get it figured out. Here's the code we ended up using.&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
    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 =&amp;gt; {

        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
            }
        })
    });
}&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 26 May 2022 14:56:58 GMT</pubDate>
    <dc:creator>Kevin_ThinkFuel</dc:creator>
    <dc:date>2022-05-26T14:56:58Z</dc:date>
    <item>
      <title>Unhandled Promise Rejection</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/626147#M843</link>
      <description>&lt;P&gt;Hi there, we are trying to recreate the referral program described in &lt;A href="https://www.linkedin.com/pulse/create-referral-program-hubspot-using-marketing-hub-jack-coldrick/" target="_blank" rel="noopener"&gt;this post&lt;/A&gt;. 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".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code we are using (referenced in the above article) can be &lt;A href="https://gist.githubusercontent.com/jackcoldrick90/b38268080650f8531650c2b47f681d8d/raw/bdf6e44b185983a15c53d384349192aeff48ae8e/referralProgramAttribution-v3.js" target="_blank" rel="noopener"&gt;found here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For reference, here is the full error we are seeing when we test the action.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="p-left-2 private-pre--code"&gt;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.&amp;lt;anonymous&amp;gt; (/opt/nodejs/node_modules/request/request.js:1154:10)","    at Request.emit (events.js:314:20)","    at IncomingMessage.&amp;lt;anonymous&amp;gt; (/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.&amp;lt;anonymous&amp;gt; (/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)"]}&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 May 2022 13:45:56 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/626147#M843</guid>
      <dc:creator>Kevin_ThinkFuel</dc:creator>
      <dc:date>2022-05-03T13:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: Unhandled Promise Rejection</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/626149#M844</link>
      <description>&lt;P&gt;Removed.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 13:47:09 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/626149#M844</guid>
      <dc:creator>Kevin_ThinkFuel</dc:creator>
      <dc:date>2022-05-03T13:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: Unhandled Promise Rejection</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/639145#M872</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Did you find a solution for this? I'm receiving the same error when I try to recreate that referral program.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 20:37:16 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/639145#M872</guid>
      <dc:creator>M_Mott</dc:creator>
      <dc:date>2022-05-25T20:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Unhandled Promise Rejection</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/639581#M875</link>
      <description>&lt;P&gt;We did get it figured out. Here's the code we ended up using.&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
    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 =&amp;gt; {

        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
            }
        })
    });
}&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 26 May 2022 14:56:58 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/639581#M875</guid>
      <dc:creator>Kevin_ThinkFuel</dc:creator>
      <dc:date>2022-05-26T14:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: Unhandled Promise Rejection</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/639726#M876</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 17:39:01 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Unhandled-Promise-Rejection/m-p/639726#M876</guid>
      <dc:creator>M_Mott</dc:creator>
      <dc:date>2022-05-26T17:39:01Z</dc:date>
    </item>
  </channel>
</rss>

