APIs & Integrations

sflht
Member

Issues with making custom code workflow

Hey all, I am making a custom code to get all the line items associated with a deal in one string and then add them to a sheet in another step and I am a bit stuck in it. 

this is the error I'm getting: 

2025-01-21T09:38:08.256Z	ERROR	Error in workflow execution: hubspotClient.crm.associations.getAll is not a function

Memory: 79/128 MB
Runtime: 753.15 ms

and here is the code:

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

exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({ accessToken: process.env.HSAPISara });

try {
// Retrieve the deal ID from the workflow event
const dealId = event.object.objectId;

// Get associated line items
const associationsResponse = await hubspotClient.crm.associations.getAll("deal", dealId, "line_item");

if (!associationsResponse || !associationsResponse.results) {
throw new Error("No associated line items found.");
}

const lineItemIds = associationsResponse.results.map(association => association.id);

// Fetch line item details
const lineItemPromises = lineItemIds.map(async lineItemId => {
const lineItemResponse = await hubspotClient.crm.lineItems.basicApi.getById(lineItemId, ["name"]);

if (!lineItemResponse || !lineItemResponse.body || !lineItemResponse.body.properties) {
throw new Error(`Line item with ID ${lineItemId} is missing properties.`);
}

return lineItemResponse.body.properties.name;
});

const lineItemNames = await Promise.all(lineItemPromises);

// Return the line item names
callback(null, {
outputFields: {
line_items: lineItemNames.join(", ")
}
});
} catch (error) {
console.error("Error in workflow execution:", error.message);
callback(error);
}
};

I would really appreciate it if anyone can help me with this.


0 Upvotes
2 Replies 2
sflht
Member

Issues with making custom code workflow

Hey @Jaycee_Lewis Thanks for the help. 
Yes I still get the error: 

2025-01-23T14:34:34.435Z	ERROR	Error in workflow execution: Cannot read properties of undefined (reading 'getAll')

Memory: 79/128 MB
Runtime: 1076.16 ms

 

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Issues with making custom code workflow

Hey, @sflht 👋 Thanks for your question. Have you tried using to get your associations:

deals.associationsApi.getAll()

 

Best,

Jaycee

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes