update a deal property based on line-item productid - custom code within a HubSpot workflow

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.

Hi @SWM,

Thank you for reaching out to the Community!

I wanted to invite a couple of subject matter experts to this thread to see if they have any advice:

Hi @Bryantworks, @louischausse, @ChrisoKlepke, do you have any tips for @SWM?

Thank you!

Best,

Kristen

Hi @kvlschaefer, Thank you for considering this.

Meanwhile, I found a solution that worked for me.
Ref - Slack

Hey
Didn’t try the following but this should work:

Replace:

const productId = lineItem.productId; console.log(productId);

By:

const productId = lineItem.toObjectId; console.log(productId);

productId is not an attribute of lineItems
Don’t forget to mark my reply as a solution if you are satisfied. If not, do not hesitate to ask me anything!

Hi Louis, Thank you for the suggestions, I found a solution that worked for me.
Ref - Slack

Solution - Slack