I have some experience in R but nearly none in JavaScript/Python. In scowering the internet for hours this is the best I’ve came up with with finding line item associations with a deal. I want to be able to use the line items and their information later in the workflow to attach to a new deal.
What am I missing?
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.hapikey
});
// First, make a call to get deal associations
hubspotClient.crm.deals.associationsApi.getAll(event.object.objectId, 'line_item').then((results) => {
let lineItemPromises = results.body.results.map(item => {
// For each line item, get its details
return hubspotClient.crm.lineItems.basicApi.getById(item.id, ['amount']).then(results => {
return results.body; // Return the details of the line item
}).catch(err => {
console.error(`Error fetching details for line item ${item.id}:`, err.message);
return null; // Return null or handle as appropriate for your needs
});
});
// Resolve all promises to get details of each line item
Promise.all(lineItemPromises).then(lineItemDetails => {
console.log(lineItemDetails); // Log the details of all line items
// Further processing can be done here with lineItemDetails
// For example, filtering high-value line items, aggregating data, etc.
callback({}); // Complete the function
}).catch(err => {
console.error('Error processing line items:', err.message);
callback({}); // Complete the function, consider passing error details
});
}).catch(err => {
console.error('Error fetching line item associations:', err.message);
callback({}); // Complete the function, consider passing error details
});
}