⚙ Operations Hub

SWM
Participant

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

SOLVE

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.

image.png

1 Accepted solution
SWM
Solution
Participant

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

SOLVE
5 Replies 5
SWM
Solution
Participant

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

SOLVE
louischausse
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

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

SOLVE

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!

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
SWM
Participant

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

SOLVE

Hi Louis, Thank you for the suggestions, I found a solution that worked for me.
Ref - https://hubspotdev.slack.com/archives/C019VT42R8X/p1683890995731059

kvlschaefer
Community Manager
Community Manager

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

SOLVE

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


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
SWM
Participant

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

SOLVE

Hi @kvlschaefer, Thank you for considering this.

 

Meanwhile, I found a solution that worked for me.
Ref - https://hubspotdev.slack.com/archives/C019VT42R8X/p1683890995731059

0 Upvotes