hubspotClient.crm.lineItems.batchApi.create Does not create associations

When setting up a custom code block, and using hubspotClient.crm.lineItems.batchApi.create NodeJs endpoint, the call returns a complete, but the association is not being set.

- I am trying to do this with line Items and deals

- The trigger is a Deal

The line item is created, the association is not when checking on the hs_object_id linked deal

Using the following code, partially taken from the dev documentation:

 // Assuming these are coming from event input fields
 const hs_object_id = event.inputFields['hs_object_id'];
 const product_quantity_1 = event.inputFields['marketplace_voucher'];
 const product_id_1 = 1162573810;

 // Define the properties for the first line item
 const properties1 = {
 "name": "Line Item Test",
 "hs_product_id": product_id_1,
 "quantity": product_quantity_1,
 };

 // Define the associations for the line items
 const associations = [
 {"to":{"id": hs_object_id},"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":20}]}];

 // Define the inputs for the create() method
 const body = { 
 inputs: [
 {
 "properties": properties1,
 "associations": associations
 }
 ] 
 };

try {
 const apiResponse = await hubspotClient.crm.lineItems.batchApi.create(BatchInputSimplePublicObjectInputForCreate);
 console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
}

Hey, @Arno-VdW :waving_hand: I’m not an expert with Node.js, but I know a. few — :waving_hand: @albertsg @MatthiasWeber @miljkovicmisa :waving_hand:

Do you have any insight? Or can you share your favourite troubleshooting tips?

I have a few questions to help give our community more context:

  • When calling the create method, are you sure the variable BatchInputSimplePublicObjectInputForCreate contains the right data? It seems like you’ve prepared the body variable for this purpose.
  • Have you tried something like this?const apiResponse = await hubspotClient.crm.lineItems.batchApi.create(body);
  • Are there any errors being returned by the catch block in your code?
  • Have you tried creating associations manually using the crm.associations.batchApi.create method after creating the line items?
  • Breaking out these steps can sometimes reveal insight into the issue

Thank you! — Jaycee

I quickly switched up my code to the older version and let it run.

Here is the full code:

const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => { 

 const hubspotClient = new hubspot.Client({"accessToken":process.env.Deal_Line_Item});

 // Assuming these are coming from event input fields
 const hs_object_id = event.inputFields['hs_object_id'];
 const product_quantity_1 = event.inputFields['marketplace_voucher'];
 const product_id_1 = 1162573810;

 const BatchInputSimplePublicObjectInputForCreate = {
 inputs: [{
 "properties":{
 "name":"Test Line Item Association",
 "hs_product_id": product_id_1,
 "quantity": product_quantity_1,
 },"associations":[{
 "to":{
 "id": hs_object_id
 },
 "types":[{
 "associationCategory":"HUBSPOT_DEFINED",
 "associationTypeId":20
 }]
 }]
 }]
 };

 try {
 const apiResponse = await hubspotClient.crm.lineItems.batchApi.create(BatchInputSimplePublicObjectInputForCreate);
 console.log(JSON.stringify(apiResponse, null, 2));
 } catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
 }
}

And here is the response:

{
 "status": "COMPLETE",
 "results": [
 {
 "id": "4695511535",
 "properties": {
 "amount": "25.000",
 "createdate": "2023-05-25T13:40:32.306Z",
 "hs_acv": "25.000",
 "hs_arr": "0.000",
 "hs_lastmodifieddate": "2023-05-25T13:40:32.306Z",
 "hs_margin": "25.000",
 "hs_margin_acv": "25.000",
 "hs_margin_arr": "0.000",
 "hs_margin_mrr": "0.000",
 "hs_margin_tcv": "25.000",
 "hs_mrr": "0.000",
 "hs_object_id": "4695511535",
 "hs_position_on_quote": "0",
 "hs_pre_discount_amount": "25.000",
 "hs_product_id": "1162573810",
 "hs_recurring_billing_number_of_payments": "1",
 "hs_tcv": "25.000",
 "hs_total_discount": "0.000",
 "name": "Test Line Item Association",
 "price": "25"
 },
 "createdAt": "2023-05-25T13:40:32.306Z",
 "updatedAt": "2023-05-25T13:40:32.306Z",
 "archived": false
 }
 ],
 "startedAt": "2023-05-25T13:40:32.292Z",
 "completedAt": "2023-05-25T13:40:32.391Z"
}

So the call is working, but if I check the associations on the deal, it is not.

This on the other hand, is my working code:

const axios = require('axios');

exports.main = async (event, callback) => { 

 const headers = {
 "Authorization": "Bearer " + process.env.Deal_Line_Item,
 "Content-Type": "application/json",
 };

 // Assuming these are coming from event input fields
 const hs_object_id = event.inputFields['hs_object_id'];

 // Define the associations for the line items
 const associations = [{"to":{"id": hs_object_id},"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":20}]}];

 // Define the properties for the line items
 var input1 = {};

 if (event.inputFields['marketplace_voucher_25'] > 0){
 const properties1 = {
 "name": "marketplace_voucher_25",
 "hs_product_id": 1162573810,
 "quantity": event.inputFields['marketplace_voucher_25'],
 };
 input1 = {
 "properties": properties1,
 "associations": associations
 }
 }

 // Define the inputs for the create() method
 const body = { 
 inputs: [
 input1
 ] 
 };

 // Make the API call
 try {
 const url = "https://api.hubapi.com/crm/v3/objects/line_items/batch/create";

 const HubspotApiResponse = await axios.post(url, body, { headers });
 if(HubspotApiResponse.data.status && HubspotApiResponse.data.status == "COMPLETE"){
 callback({
 outputFields: {
 customCodeSuccess: true,
 customCodeOutput: "Line item added succesfully"
 }
 });
 return;

 }
 } catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
 }
}

Hope this helps!

Hi Everyone,

I know this is an older thread, but I wanted to contribute a solution for anyone who might be facing similar issues in the future. I’ve encountered this problem with clients before, and often, when we reviewed the variables using console.log(), we discovered that ‘hs_object_id’ had not resolved before ‘BatchInputSimplePublicObjectInputForCreate’ was set. Another issue was that ‘hs_object_id’ was sometimes being set as an integer instead of a string.

To troubleshoot this, I recommend using

console.log(JSON.stringify(BatchInputSimplePublicObjectInputForCreate))

to ensure everything is behaving as expected. This step should help you pinpoint the cause of any issues.

Ah, I see now I copied the code wrongfully. I was indeed using the BatchInputSimplePublicObjectInputForCreate but I switched to using Axios instead afterwards so halfly copy pasted from my existing code.

The weird thing is, I am getting the confirmation the line item is being created, but the association is not being made nor any errors on this.

And when I try it it postman, or in custom code, via a normal API call, it just works like a charm.

Thanks for the answer nonetheless!