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!