Workflow custom code - doSearch not returning results

We are trying to retrieve a contact to update by using a custom parameter that is passed into the custom code section of a workflow.

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

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

const filterGroup = {
filters: [{
propertyName: ‘referrer_id’,
operator: ‘EQ’,
value: referrer_
}]
}

const properties = [‘referrer_id’, ‘total_referrals’, ‘firstname’, ‘lastname’, ‘email’]

const publicObjectSearchRequest = {
filterGroups: [filterGroup],
properties
}

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

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;

}

The referrer_ variable contains the correct value but the doSearch never returns any results. I’ve also tried just searching for the contact by email with the correct email hardcoded into the query but i still receive nothing back. Is there something else i should be doing?

Hi, @LDev7 :waving_hand: Let’s dig into the search operator part of things, and we can go from there :racing_car:

The EQ filter is strict. Unless you literally have a Contact with an exact value of “referrer_” for the Referrer ID property, I’m not surprised it’s coming back with zero results.

Can you try the CONTAINS_TOKEN operator instead, please?

I ran a quick test and got back a successful result.
PS, I made a few assumptions on your exact setup, but I think I got close:

  • I created a custom property called “referrer id” and gave it an internal value of referrer_id

  • I assigned this property to three contacts. Not only that, but I wasn’t certain if you were concatenating an additional string to each Contact’s value for the property — meaning I chose to do it like “referrer_id_123”, for example.

  • My Search API request (using Postman)
    Endpoint https://api.hubapi.com/crm/v3/objects/contacts/search
    Request body

    {
     "properties": [
     "referrer_id",
     "firstname",
     "lastname",
     "email"
     ],
     "filterGroups": [
     {
     "filters": [
     {
     "propertyName": "referrer_id",
     "operator": "CONTAINS_TOKEN",
     "value": "referrer_"
     }
     ]
     }
     ]
    }​
    
  • Response

    {
     "total": 3,
     "results": [
     {
     "id": "801",
     "properties": {
     "createdate": "2022-11-03T21:42:31.255Z",
     "email": "louise@bobsburgers.com",
     "firstname": "Louise",
     "hs_object_id": "801",
     "lastmodifieddate": "2023-03-02T21:56:07.468Z",
     "lastname": "Belcher",
     "referrer_id": "referrer_A_VALUED_PARTNER"
     },
     "createdAt": "2022-11-03T21:42:31.255Z",
     "updatedAt": "2023-03-02T21:56:07.468Z",
     "archived": false
     },
     {
     "id": "851",
     "properties": {
     "createdate": "2022-11-03T21:42:31.255Z",
     "email": "linda@bobsburgers.com",
     "firstname": "Linda",
     "hs_object_id": "851",
     "lastmodifieddate": "2023-03-02T21:35:40.362Z",
     "lastname": "Belcher",
     "referrer_id": "referrer_456"
     },
     "createdAt": "2022-11-03T21:42:31.255Z",
     "updatedAt": "2023-03-02T21:35:40.362Z",
     "archived": false
     },
     {
     "id": "901",
     "properties": {
     "createdate": "2022-11-03T21:42:31.255Z",
     "email": "bob@bobsburgers.com",
     "firstname": "Bob",
     "hs_object_id": "901",
     "lastmodifieddate": "2023-03-02T21:33:25.050Z",
     "lastname": "Belcher",
     "referrer_id": "referrer_123"
     },
     "createdAt": "2022-11-03T21:42:31.255Z",
     "updatedAt": "2023-03-02T21:33:25.050Z",
     "archived": false
     }
     ]
    }​
    
  • These three Contacts match the three shown in my filtered view

If I missed anything, or you have additional details to share, please let me know!

Have fun building! — Jaycee

Hi Jaycee,

Thanks for the reply. The referrer_ value is a variable that is assigned a value from the input field. In this case a 5 digit number. This number matches exactly with the referrer_id property on the contact record.

This appears to be working now. The issue i now have is retrieving the data from the results. I would like the firstname property in the result to add to a variable.

An example i had been given was to use: let referred_by = results.body.results[0].properties.firstname

but when doing this i get this error

"errorMessage":"TypeError: Cannot read properties of undefined (reading 'results')"

If i just do a console.log of the results i get this:

CollectionResponseWithTotalSimplePublicObjectForwardPaging {
 total: 1,
 results: [
 SimplePublicObject {
 id: '121129001',
 properties: [Object],
 createdAt: 2022-08-11T07:07:09.744Z,
 updatedAt: 2023-03-02T12:28:02.242Z,
 archived: false
 }
 ]
}

Is there a way to access the properties to use them in the custom code?

Thanks again

Ah i’ve got it, just need to remove the .body from the code.

So instead of let referred_by = results.body.results[0].properties.firstname

it should be let referred_by = results.results[0].properties.firstname.

thanks for your help, it all appears to be working now