Updating Line Items via Custom Code Workflow Step

Hello,
I’m trying to update past line items with specific checkboxes so I can easily use them for reports and calculate commissions.
Here’s GitHub to the code I’m using.

The code runs, but does not like the “.basicApi” on line 21. Does this update method not work on line items? I was able to use this method to update tickets and meetings.
Shout out to Zach Klein for showing me the the method I’ve been able to use.

Hello @DHills48

The error message: TypeError: Cannot read properties of undefined (reading ‘basicApi’) usually means there is something wrong with the previous object which is “line_items”.

In the HubSpot line items are accessed via hubspotClient.crm.lineItems instead of hubspotClient.crm.line_items.

Reference: https://developers.hubspot.com/docs/reference/api/crm/objects/line-items#basic

Was I able to help answer your question? Help the community by marking it as a solution.

@WesQ Thank you! I’m passed the first hurdle. Now it the error I’m getting is the “lineItemID is null or undefined”.
I take that team it’s not getting line item’s id/record number, which is surprising to me because I am pulling it in as an input field. I also updated my code to match the errors specification.
I updated GitHub with the latest code I’m using.

DHills48_1-1738626735574.png

The first argument required by the lineItems.basicApi.update is the lineItemId that you want to update.

const apiResponse = await hubspotClient.crm.lineItems.basicApi.update(lineItemId, SimplePublicObjectInput, idProperty);

On your code you are using record_id as the first argument and its value is undefined because as seen on your picture attached you are getting the record Id and giving it a name of hs_object_id. If you change it to record_id it should work.

Reference:

https://developers.hubspot.com/docs/reference/api/crm/objects/line-items#patch-%2Fcrm%2Fv3%2Fobjects%2Fline_items%2F%7Blineitemid%7D

Let me know if you need more help. And please mark my answer as solution. Thank you!

Hi @WesQ it’s still returning an error.
If I understood correctly, I was supposed to change the input field from “hs_object_id” to “record_id” correct?
I saw in the documentation it said the “Scope” was e-Commerce. It should work as long as it as crm.objects.line_items.write and .read, right?

@DHills48

Yes, you correctly changed the input field name to match what’s inside the code.

For the scope, the granular scope "crm.objects.line_items.write ", would be enough. Dont need the e-commerce.

Can you post the new error you are getting now?

@WesQ Yep! Here’s the full error.

@DHills48

With that error, it is most probable that you are not getting the lineItemId value correctly. How are you getting the line item Id that you want to update?

To debug this I would usually get a sample line item id that I want to update from the CRM UI like this:

Then plug the string value as the value for the lineItemId like shown in line 7:

Then use the “Test” button to see if it works:

It will most probably work. Then next question would be, how are you going to get the line Id you want to update. Can you show me your workflow trigger? Is this workflow deal based?

Hi @WesQ,
Sorry for the delay. Thank you for the troubleshooting example!
Yes, the workflow is deal based. Here’s a screenshot of the enrollment criteria.

@DHills48

As a recap, what we need to fetch first are the lineItemIds to update right?

And right now since we have a deal based workflow here, I believe there is no currently available “workflow action” that can get value of all the line items associated. If your deals have a 1 to 1 relationship to line items you can use “Property to include in code” in the action, however you most probably have multiple line items associated with the enrolled deal so this won’t work.

Solution:

In your custom code action, you need to first

  1. Fetch all the associated line items you need to update.

  2. Loop through each line item and use API to update each individually.

See section “Retrieve Associated Records” in the reference below on how to do that.

Reference:

Was I able to help answer your question? Help the community by marking it as a solution.