Difficulty detecting duplicate Contacts

Hi

Our system pulls all our local users into batches and will then query, in batches, for those Contacts on HubSpot. This is done using the Batch API for speed.

We can ask for all Contacts for a given set of Email addresses.

This is fine, but the problem comes when we then try and create those Contacts. If the email address was not their primary they will not come back in the search. Our system will then try to create but if the email addresses is listed as an additional email, the API will return a CONFLICT - Contact already exists.

Because we are using the Search API I cannot see a nice way of querying for batches of 100 users whlist also taking into account hs_additional_emails.

If I ask for a user by Email and the API says they do not exist, I should be able to them create that Contact. I should not have to query multiple email fields to see if they truly exist.

This seems like bad API design.

It would also be nice if the actual error told me which, from a batch of 100 email addresses, which one was the actual ‘duplicate’ rather than the useless ‘Contact already exists’ - I am creating a batch!

I’m confused on whether you’re querying the list of user emails via Batch API or Search API since you mention both.

If you are using Search API, you can use filter groups and the IN operator (and put the emails in an array) on a property to run the search. I’m not sure if there’s a limit on how many items can be in an array.

Eg,

This has two OR filters that searches the email field and work_email field and will return records if any result is true.

{
 "filterGroups": [
 {//Searches the email field
 "filters": [
 {
 "propertyName": "email",
 "operator": "IN",
 "values": ["bh@hubspot.com", "emailmaria@hubspot.com"]
 }
 ]
 },
 {//Searches the work_email field
 "filters": [
 {
 "propertyName": "work_email",
 "operator": "IN",
 "values": ["red@hubspot.com", "blue@hubspot.com"]
 }
 ]
 }
 ]
}

I do agree with you that Batch API errors could be a lot more useful.

Thanks for the reply - sadly this setup doesnt quite work for me as basically I have batches of 100 email addresses. I am using the search API to find email addresses using the IN filter, but this only searches the primary email.

If I also want Contacts via their hs_additional_emails field I have to supply more filters where the field CONTAINS the email. Due to the limitations of the API I can’t really constructs the query I need - unless I am doing it wrong.

For simplicity, imagine you have 1000 users on your system - how do you find them in batch using the API? You need to search both the email field and the additional email comma separated list.

I got so caught up in the idea of doing this in one query that I didn’t consider I could do it in two. So I think i’ll go that route. I think ideally there should be a Contact API endpoint whereby you can search on Email address and the system abstract away the specific storage detail (primary or additional) and gives you the relevant Contacts back.

I really don’t understand the issue?

If you’re using search API to find email addresses using the IN filter, you can have filter groups that searches multi properties like the code example I added. It would be the same filter you’re using but adding another filter to the filter group to utilize the OR property of multiple filters.

Below would be a filter that searches in the email, work_email and personal_email field. If it appears in any one, the record will be returned.

{
 "filterGroups": [
 {//Searches the email field
 "filters": [
 {
 "propertyName": "email",
 "operator": "IN",
 "values": ["bh@hubspot.com", "emailmaria@hubspot.com"]
 }
 ]
 },
 {//Searches the work_email field
 "filters": [
 {
 "propertyName": "work_email",
 "operator": "IN",
 "values": ["bh@hubspot.com", "emailmaria@hubspot.com"]
 }
 ]
 },
 {//Searches the work_email field
 "filters": [
 {
 "propertyName": "personal_email",
 "operator": "IN",
 "values": ["bh@hubspot.com", "emailmaria@hubspot.com"]
 }
 ]
 }
 ]
}

You can’t do an IN on hs_additional_emails because it is not a single value but a comma separated list. So you need to use CONTAINS, but you can’t provide an array of values here, so you must do them one by one.

hs_additional_emails is an enumerated field. You can use IN operator on it. While it may be stored in hubspot as a deliminated list, if you use the API/GraphQL, you can use the IN operator.

I just tested via the PostMan using the search API. I added a bunch of emails to the default record for Maria Johnson.

Oh! Thank you for the info!

Is this documented anywhere?

They have an example on their help document on the Search API under “Filter search results” on using the IN operator on enumerated fields (search for enumeration_property on the page and you’ll see the code).

Once you know that, it’s just verifying that hs_additional_emails is also an enumerated field.

Thanks - I guess the key is knowing its an enumerated field!