Create Line Items for a Deal during a Workflow Custom Code Block
SOLVE
Goal is to add a set of line_items to a deal using a workflow custom code block. A set of SKUs will be passed in. Approach is to add the line items, and then associate them with the deal. The Products already exist in the product object, so we can reference hs_sku or hs_product_id as required.
Getting an error when running the crm.line_items.create method.
It looks like the crm.line_items methods are being blocked or erroring.
Can anyone assist?
Details:
Workflow: After deal created, this workflow and action is run
Language: node.js 16.x
Private App: has read and write access to crm.deals and crm.line_items
Client: Hubspot Client v8
Custom Code:
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const skus = event.inputFields['expedition_skus_specification'];
const dealId = event.object.objectId;
console.log('Product SKUs to add to the deal: ' + skus);
console.log('Deal ID: ' + dealId);
const hubspotClient = new hubspot.Client({
accessToken: process.env.ProductConfiguratorKey
});
console.log('Reading deals properties');
let amount;
try {
const ApiResponse = await hubspotClient.crm.deals.basicApi.getById(event.object.objectId, ["amount"]);
amount = ApiResponse.properties.amount;
console.log('Retrieved deal amount is: ' + amount);
} catch (err) {
console.error(err);
throw err;
}
console.log('Creating a line item');
const lineItemInput = {
name: 'Main product',
quantity: 1,
price: 140000.0
//hs_product_id: hs_product_id // Replace with the existing hs_product_id
// other line item properties as needed
};
//hubspotClient.crm.line_items.basicApi.create(lineItemInput)
hubspotClient.crm.line_items.create(lineItemInput)
.then((createdLineItem) => {
console.log('Created line item with ID:', createdLineItem.id);
// do something with the created line item
})
.catch((err) => {
console.error(err);
});
callback({
outputFields: {
expedition_skus_returned: skus
}
});
}
Error:
2023-04-07T11:10:49.490Z INFO Product SKUs to add to the deal: undefined
2023-04-07T11:10:49.490Z INFO Deal ID: 1127594795
2023-04-07T11:10:49.491Z INFO Reading deals properties
2023-04-07T11:10:50.832Z INFO Retrieved deal amount is: 30000
2023-04-07T11:10:50.832Z INFO Creating a line item
2023-04-07T11:10:50.889Z ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'create')","reason":{"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'create')","stack":["TypeError: Cannot read properties of undefined (reading 'create')"," at Object.exports.main (/var/task/file.js:36:32)"," at processTicksAndRejections (node:internal/process/task_queues:96:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'create')"," at process.<anonymous> (file:///var/runtime/index.mjs:1188:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:140:20)"," at processPromiseRejections (node:internal/process/promises:274:27)"," at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}
Unknown application error occurred
Runtime.Unknown
Memory: 25/128 MB
Runtime: 1599.94 ms