Hello Everyone,
I am developing a Custom code that is designed to retrieve line items associated with a deal and based on certain conditions of line-item, update a specific property of the deal with an incremental value.
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.SECRET_NAME
});
const dealId = event.object.objectId; console.log(dealId);
try {
const lineItemsResponse = await hubspotClient.crm.deals.associationsApi.getAll(dealId, 'line_item');
const lineItems = lineItemsResponse.results; console.log(lineItems);
let increaseBy = 0;
for (const lineItem of lineItems) {
const productId = lineItem.productId; console.log(productId);
if (productId === "2032483348") {
increaseBy = 27;
} else if (productId === "2032483347") {
increaseBy = 20;
} else if (productId === "2032483346") {
increaseBy = 15;
} else if (productId === "2032203285") {
increaseBy = 10;
} else if (productId === "2032483344") {
increaseBy = 5;
}
const dealResponse = await hubspotClient.crm.deals.basicApi.getById(dealId);
const deal = dealResponse.body;
if (deal && deal.properties) {
const IncreaseValue = deal.properties.increase_value || 0;
const dealUpdateRequest = {
properties: {
increase_value: IncreaseValue + increaseBy
}
};
await hubspotClient.crm.deals.basicApi.update(dealId, dealUpdateRequest);
}
}
callback({});
} catch (err) {
console.error(err);
throw err;
}
};
I am able to get a list of associated Line items, but not the details of the line item (name, ProductID etc), Please let me know if you have any solutions.
