Im new to hubspot crm, but trying to make one simple thing. I have created the extension that just make document files, now i need data for this files. I want to get it from Deal associations. I have custom object - it represents the products associated with the deal. So, everything I want just to fetch the list of the products.
My card use call to serverless function, main data it passes without problem, so I assume I make it works. But I stuck with getting the products. I tried this request right in the sandobx of documenation /crm/v4/objects/{objectType}/{objectId}/associations/{toObjectType}
I put my token and objectType, objectId and toObjectType to the sandbox and it works. I see the results in sandbox below. So, I copied the code from it to my serverless function and I can't make a request, because getting ERROR - TypeError: Cannot read properties of undefined (reading 'basicApi') at exports.main (/var/task/test.js:66:47) at exports.hubspot_handler [as handler] (/var/task/hubspotHandler.js:20:34) at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1187:29)
I google a lot and find that there was a problem with Node.js, but it was in 2023. So, I hope they have fixed it already. I tried to create a simple fetch requests to v3 and v4 api and didn't get results in it. All the times some kind of errors. So, can somebody please share how you guys fetching the associations details with hubspot SDK or without.
This is the code I'm using on the CRM Card I'm building (it's a serverless function but the code should be fine anywhere). In general, I prefer to use BatchApi over BasicApi because I can keep the data format consistent across all functions (instead of using one for when I retrieve record and another when I retrieve multiple records). I have no idea if there's a speed benefit for one vs the other.
Media Plans is our custom object which is converted to the p_media_plans object name. So you can see the first function, getTicketAssociationsById, simply prepares the payload (ticket id) and then calls the function to retrieve the data. All the data gets returned to the function that called it.
/*****
Retrieve all associations of a ticket that we need
*****/
async function getTicketAssociationsById (ticket_id) {
const payload = {"inputs": [ { "id": ticket_id }]};
let companies = await getAssociations( "tickets", "company", payload );
let contacts = await getAssociations( "tickets", "contacts", payload );
let deals = await getAssociations( "tickets", "deals", payload );
let media_plans = await getAssociations( "tickets", "p_media_plans", payload );
return ( {"companies": companies, "contacts": contacts, "deals": deals, "media_plans": media_plans } );
}
/*****
Associations Call
*****/
async function getAssociations (source, target, data) {
// API Call
try {
const results = await hubspotClient.crm.associations.v4.batchApi.getPage(source, target, data)
return results.results
} catch (e) {
console.log(e)
}
}
Since I don't know your code that is returning the error, hard to tell what's wrong. But unknown function usually means there's something wrong in the function you're trying to call.
If the code that you pulled is not working, which it should, you probably need to verify the data you're presenting.
I would open up something like Postman and test the API request directly.
This is the code I'm using on the CRM Card I'm building (it's a serverless function but the code should be fine anywhere). In general, I prefer to use BatchApi over BasicApi because I can keep the data format consistent across all functions (instead of using one for when I retrieve record and another when I retrieve multiple records). I have no idea if there's a speed benefit for one vs the other.
Media Plans is our custom object which is converted to the p_media_plans object name. So you can see the first function, getTicketAssociationsById, simply prepares the payload (ticket id) and then calls the function to retrieve the data. All the data gets returned to the function that called it.
/*****
Retrieve all associations of a ticket that we need
*****/
async function getTicketAssociationsById (ticket_id) {
const payload = {"inputs": [ { "id": ticket_id }]};
let companies = await getAssociations( "tickets", "company", payload );
let contacts = await getAssociations( "tickets", "contacts", payload );
let deals = await getAssociations( "tickets", "deals", payload );
let media_plans = await getAssociations( "tickets", "p_media_plans", payload );
return ( {"companies": companies, "contacts": contacts, "deals": deals, "media_plans": media_plans } );
}
/*****
Associations Call
*****/
async function getAssociations (source, target, data) {
// API Call
try {
const results = await hubspotClient.crm.associations.v4.batchApi.getPage(source, target, data)
return results.results
} catch (e) {
console.log(e)
}
}
Since I don't know your code that is returning the error, hard to tell what's wrong. But unknown function usually means there's something wrong in the function you're trying to call.
If the code that you pulled is not working, which it should, you probably need to verify the data you're presenting.
I would open up something like Postman and test the API request directly.
Have a beautiful day and thanks so much for your help! Bérangère
HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates. Learn More.