<?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: Custom Code Action Issue - contact property returning undefined in Data Hub</title>
    <link>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963881#M2274</link>
    <description>&lt;P&gt;I've got futher along by taking a different approach with the property values, since I don't really see the need to get the contact object and interrogate the properties that way, when they are immediately available to me do to contact enrollment in the workflow.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;So I'm pulling them using `event.inputFields`&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then pass the 3 property names and their values as 3 different search filter groups. However, the next 4 lines of code after that throw an undefined error again. Since I can't tell in this code editor what has actually been returned by the search it's hard to tell what's wrong.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;/**
 * Searches for another contact with the same value of DEDUPE_PROPERTY.
 * - If no matches are found, nothing happens
 * - If one match is found, the enrolled contact is merged into the matching contact
 * - If more than one match is found, the action fails
 */

const hubspot = require('@hubspot/api-client');

  const DEDUPE_PROPERTY1 = 'firstname';
  const DEDUPE_PROPERTY2 = 'lastname';
  const DEDUPE_PROPERTY3 = 'company';
  const PROPERTY_USED_TO_DEDUPE_CONTACTS = 'phone';

exports.main = (event, callback) =&amp;gt; {
  // Make sure to add your API key under "Secrets" above.
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.mContact
  });

  hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, ['email'])
    .then(contactResult =&amp;gt; {

    const firstNameVal = event.inputFields.firstname;
    const lastNameVal = event.inputFields.lastname;
    const companyNameVal = event.inputFields.company;

    console.log(`Looking for duplicates based on ${DEDUPE_PROPERTY1} = ${firstNameVal} &amp;amp;&amp;amp; ${DEDUPE_PROPERTY2} = ${lastNameVal} &amp;amp;&amp;amp; ${DEDUPE_PROPERTY3} = ${companyNameVal}`);
      hubspotClient.crm.contacts.searchApi
        .doSearch({
          filterGroups: [{
            filters: [
              {
                propertyName: 'firstname',
                operator: 'EQ',
                value: firstNameVal
              },
              {
                propertyName: 'lastname',
                operator: 'EQ',
                value: lastNameVal
              },
              {
                propertyName: 'company',
                operator: 'EQ',
                value: companyNameVal
              }
            ]
          }]
        })
        .then(searchResults =&amp;gt; {
          let idsToMerge = searchResults.body.results
            .map(object =&amp;gt; object.id)
            .filter(vid =&amp;gt; Number(vid) !== Number(event.object.objectId));
	});
  });
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the error returned:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;2024-04-20T05:27:56.674Z	INFO	Looking for duplicates based on firstname = Taylor &amp;amp;&amp;amp; lastname = Newman &amp;amp;&amp;amp; company = Flight Digital
2024-04-20T05:27:56.897Z	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'results')","reason":{"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'results')","stack":["TypeError: Cannot read properties of undefined (reading 'results')","    at /var/task/file.js:53:47","    at processTicksAndRejections (node:internal/process/task_queues:96:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'results')","    at process.&amp;lt;anonymous&amp;gt; (file:///var/runtime/index.mjs:1276:17)","    at process.emit (node:events:513:28)","    at emit (node:internal/process/promises:140:20)","    at processPromiseRejections (node:internal/process/promises:274:27)","    at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}
Unknown application error occurred
Runtime.Unknown&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Those filter settings should be returning two contacts in the results:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SMoyse_1-1713590971052.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/115732i675C278CB7CDC27F/image-size/large?v=v2&amp;amp;px=999" role="button" title="SMoyse_1-1713590971052.png" alt="SMoyse_1-1713590971052.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 20 Apr 2024 05:29:44 GMT</pubDate>
    <dc:creator>sMoyse</dc:creator>
    <dc:date>2024-04-20T05:29:44Z</dc:date>
    <item>
      <title>Custom Code Action Issue - contact property returning undefined</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963862#M2272</link>
      <description>&lt;P&gt;Hi There,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wasn't sure if I should put this in the API or Ops Hub location... I chose the latter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to dedupe contacts based on firstname, lastname and company. Mainly to take care of duplicate contacts as a result of merging two Companies.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've started with the dedupe based on phone number sample and changed it to suit my needs. But the property values keep returning as undefined and I can't figure out why.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm just wondering if someone on here can tell what's wrong?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SMoyse_0-1713587076986.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/115726i15F28ABB77FA2025/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SMoyse_0-1713587076986.png" alt="SMoyse_0-1713587076986.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const hubspot = require('@hubspot/api-client');

  const DEDUPE_PROPERTY1 = 'firstname';
  const DEDUPE_PROPERTY2 = 'lastname';
  const DEDUPE_PROPERTY3 = 'company';

exports.main = (event, callback) =&amp;gt; {
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.mContact
  });

  hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, ['email'])
    .then(contactResult =&amp;gt; {
      let dedupePropValue1 = contactResult.properties[DEDUPE_PROPERTY1];
      let dedupePropValue2 = contactResult.properties[DEDUPE_PROPERTY2];
      let dedupePropValue3 = contactResult.properties[DEDUPE_PROPERTY3];
      console.log(`running`)
      console.log(`Looking for duplicates based on ${DEDUPE_PROPERTY1} = ${dedupePropValue1} &amp;amp;&amp;amp; ${DEDUPE_PROPERTY2} = ${dedupePropValue2} &amp;amp;&amp;amp; ${DEDUPE_PROPERTY3} = ${dedupePropValue3}`);
  });
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also am I missing something, or is there zero documentation covering all the available objects, functions and properties accessible via Javascript? The API documentation is obviously quite different... unless I'm a complete muppet.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Apr 2024 04:26:44 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963862#M2272</guid>
      <dc:creator>sMoyse</dc:creator>
      <dc:date>2024-04-20T04:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Code Action Issue - contact property returning undefined</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963867#M2273</link>
      <description>&lt;P&gt;If I add a callback to the end of the code, then all the values return as expected:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    callback({
      outputFields: {
        firstname: event.inputFields.firstname,
        lastname: event.inputFields.laststname,
        company: event.inputFields.company,
        email: event.inputFields.email
      }
    });&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SMoyse_0-1713587853515.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/115727iD7A395A5B1B3A024/image-size/large?v=v2&amp;amp;px=999" role="button" title="SMoyse_0-1713587853515.png" alt="SMoyse_0-1713587853515.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm guessing there is something wrong with this code `&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;contactResult.properties&lt;/LI-CODE&gt;&lt;P&gt;But it works in the dedupe by phone number sample.&lt;BR /&gt;&lt;BR /&gt;I've been assuming this block of code returns the Contact Object itself and then I can interrogate it's properties, but maybe that isn't what's happening.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;  hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, ['email'])
    .then(contactResult =&amp;gt; {}&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 20 Apr 2024 04:44:42 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963867#M2273</guid>
      <dc:creator>sMoyse</dc:creator>
      <dc:date>2024-04-20T04:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Code Action Issue - contact property returning undefined</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963881#M2274</link>
      <description>&lt;P&gt;I've got futher along by taking a different approach with the property values, since I don't really see the need to get the contact object and interrogate the properties that way, when they are immediately available to me do to contact enrollment in the workflow.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;So I'm pulling them using `event.inputFields`&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then pass the 3 property names and their values as 3 different search filter groups. However, the next 4 lines of code after that throw an undefined error again. Since I can't tell in this code editor what has actually been returned by the search it's hard to tell what's wrong.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;/**
 * Searches for another contact with the same value of DEDUPE_PROPERTY.
 * - If no matches are found, nothing happens
 * - If one match is found, the enrolled contact is merged into the matching contact
 * - If more than one match is found, the action fails
 */

const hubspot = require('@hubspot/api-client');

  const DEDUPE_PROPERTY1 = 'firstname';
  const DEDUPE_PROPERTY2 = 'lastname';
  const DEDUPE_PROPERTY3 = 'company';
  const PROPERTY_USED_TO_DEDUPE_CONTACTS = 'phone';

exports.main = (event, callback) =&amp;gt; {
  // Make sure to add your API key under "Secrets" above.
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.mContact
  });

  hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, ['email'])
    .then(contactResult =&amp;gt; {

    const firstNameVal = event.inputFields.firstname;
    const lastNameVal = event.inputFields.lastname;
    const companyNameVal = event.inputFields.company;

    console.log(`Looking for duplicates based on ${DEDUPE_PROPERTY1} = ${firstNameVal} &amp;amp;&amp;amp; ${DEDUPE_PROPERTY2} = ${lastNameVal} &amp;amp;&amp;amp; ${DEDUPE_PROPERTY3} = ${companyNameVal}`);
      hubspotClient.crm.contacts.searchApi
        .doSearch({
          filterGroups: [{
            filters: [
              {
                propertyName: 'firstname',
                operator: 'EQ',
                value: firstNameVal
              },
              {
                propertyName: 'lastname',
                operator: 'EQ',
                value: lastNameVal
              },
              {
                propertyName: 'company',
                operator: 'EQ',
                value: companyNameVal
              }
            ]
          }]
        })
        .then(searchResults =&amp;gt; {
          let idsToMerge = searchResults.body.results
            .map(object =&amp;gt; object.id)
            .filter(vid =&amp;gt; Number(vid) !== Number(event.object.objectId));
	});
  });
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the error returned:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;2024-04-20T05:27:56.674Z	INFO	Looking for duplicates based on firstname = Taylor &amp;amp;&amp;amp; lastname = Newman &amp;amp;&amp;amp; company = Flight Digital
2024-04-20T05:27:56.897Z	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'results')","reason":{"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'results')","stack":["TypeError: Cannot read properties of undefined (reading 'results')","    at /var/task/file.js:53:47","    at processTicksAndRejections (node:internal/process/task_queues:96:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'results')","    at process.&amp;lt;anonymous&amp;gt; (file:///var/runtime/index.mjs:1276:17)","    at process.emit (node:events:513:28)","    at emit (node:internal/process/promises:140:20)","    at processPromiseRejections (node:internal/process/promises:274:27)","    at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}
Unknown application error occurred
Runtime.Unknown&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Those filter settings should be returning two contacts in the results:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SMoyse_1-1713590971052.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/115732i675C278CB7CDC27F/image-size/large?v=v2&amp;amp;px=999" role="button" title="SMoyse_1-1713590971052.png" alt="SMoyse_1-1713590971052.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Apr 2024 05:29:44 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963881#M2274</guid>
      <dc:creator>sMoyse</dc:creator>
      <dc:date>2024-04-20T05:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Code Action Issue - contact property returning undefined</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963913#M2275</link>
      <description>&lt;P&gt;Changing&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let idsToMerge = searchResults.body.results&lt;/LI-CODE&gt;&lt;P&gt;to&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let idsToMerge = searchResults.results&lt;/LI-CODE&gt;&lt;P&gt;fixed this issue.&lt;BR /&gt;&lt;BR /&gt;The rest of my problems with async problems.... so I got rid of the .then approach and replaced it with await.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Apr 2024 10:59:02 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Custom-Code-Action-Issue-contact-property-returning-undefined/m-p/963913#M2275</guid>
      <dc:creator>sMoyse</dc:creator>
      <dc:date>2024-04-20T10:59:02Z</dc:date>
    </item>
  </channel>
</rss>

