Basic custom code for accessing timeline

Hi,

I’ve got a company custom code workflow which does a simple summary of company properties and associated objects, and pushes them into an at-a-glance ‘tear sheet’ property.

It’s basically using the company ID to get associations to contacts, deals, etc. and then filtering out highlights.

All works fine until I get to trying to find the last emails on a company. I’m trying to access the ‘get email by ID’ endpoint, as documented here:

With this:

//First, make a call to get company associations to emails:

hubspotClient.crm.companies.associationsApi.getAll(event.object.objectId, ‘emails’).then((results) => {
let emailPromises = results.body.results.map(item => {
return hubspotClient.crm.objects.emails.basicApi.getById(item.id, [‘hs_timestamp’,‘hs_email_subject’]).then(results => {
return results.body
})
})

which doesn’t like the getById definition:

ERROR TypeError: Cannot read properties of undefined (reading 'getById')

As far as I can see, I’m using the same definition as in the API docs, which states: const apiResponse = await hubspotClient.crm.objects.emails.basicApi.getById(emailId, properties, …)

Any ideas gratefully received!

Phil

Hey, @PBaxter :waving_hand: Thanks for reaching out. Let’s invite some of our community members to the conversation — @albertsg @miljkovicmisa, do you have any troubleshooting tips you can share with @PBaxter?

Looking at the documentation example they linked to, I’m not spotting any quick red flags with the provided code snippet:

From the documentation:

const hubspot = require('@hubspot/api-client');

const hubspotClient = new hubspot.Client({"accessToken":"YOUR_ACCESS_TOKEN"});

const emailId = "emailId";
const properties = undefined;
const propertiesWithHistory = undefined;
const associations = undefined;
const archived = false;
const idProperty = undefined;

try {
 const apiResponse = await hubspotClient.crm.objects.emails.basicApi.getById(emailId, properties, propertiesWithHistory, associations, archived, idProperty);
 console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
}

Original snippet:

//First, make a call to get company associations to emails:

hubspotClient.crm.companies.associationsApi.getAll(event.object.objectId, 'emails').then((results) => {

let emailPromises = results.body.results.map(item => {
return hubspotClient.crm.objects.emails.basicApi.getById(item.id, ['hs_timestamp','hs_email_subject']).then(results => {
return results.body
})
})

Thanks for taking a look! — Jaycee