I have a workflow that sends a slack message with the quantity in line items associated with deals.
Sometimes it pulls in quantity from the wrong deal. Other times, it is right. Not sure why but its proving difficult to diagnose
I’m fairly confident this is a bug but if not, if someone can help me diagnose this further, I’ll buy you a beer (or coffee)!
Happy to provide more info
“Property Value equals” will search ALL Line Items in your CRM not just the ones associated with your deal. So it will search for SKU = SVC125 most recently updated which may or may not be associated with your current Deal.
I’m not sure if there’s a way to limit objects that are associated + with a filter. It’s one the (huge) limitations of Workflows at the moment. You can easily do it via a Custom Coded Action.
Eg,
// HubSpot API Client
const hubspot = require('@hubspot/api-client');
// Global HubSpot Client variable
let hubspotClient;
// Main Loop Starts Here
exports.main = async (event, callback) => {
// Initialise the HubSpot Client
hubspotClient = new hubspot.Client({
accessToken: process.env.WORK_FLOW_KEY_NAME_001,
numberOfApiCallRetries: 5,
});
// Retrieve the LINE ITEM ids from the DEAL
let lineItemsID = await getLineItemsFromDeal(event.object.objectId);
// Create an Object with inputs array for storing line Item IDs
let lineItemIDArray = {"inputs": []};
// Store the LineItem IDs in LineItemsIDArray
for (let lineItemID of lineItemsID) {
lineItemIDArray.inputs.push({id: lineItemID.toObjectId});
}
// Use the Line Items ID Array to fetch the Line Items Data in Batch
let lineItemsData = await getLineItemsDataBatch(lineItemIDArray);
let lineItemQuantity = 0;
// Store the LineItem IDs in LineItemsIDArray
for (let lineItemData of lineItemsData) {
console.log("SKU", lineItemData.properties.hs_sku);
console.log("Quantity", lineItemData.properties.quantity);
if (lineItemData.properties.hs_sku == 'SVC125') {
lineItemQuantity += Number(lineItemData.properties.quantity);
}
}
// Output Values
callback({
outputFields: {
lineItemQuantity: lineItemQuantity,
}
});
}
// Main Loop Ends Here
/*****
API call to get all LINE ITEMS associated with the DEAL.
Requires:
event = event.object.objectId (DEAL ID)
Output:
lineItemsData
*****/
async function getLineItemsFromDeal (objectID) {
// API call
try {
// Get All Line Items in Batch associated with the Deal by Object ID
const request = await hubspotClient.crm.associations.v4.basicApi.getPage('deals', objectID, 'line_item');
return request.results;
} catch (e) {
console.log(e);
}
}
/*****
Function to execute the API call to get data from the LINE ITEM.
Requires:
data = Line Item IDs
Output:
lineItemsData
*****/
async function getLineItemsDataBatch (data) {
// Specify which Properties to Fetch in Batch for Each Line Item
data['properties'] = [
"name",
"hs_sku",
"quantity"
];
try {
// Read the Line Items in Batch with the Specified Data Properties
const request = await hubspotClient.crm.lineItems.batchApi.read(data);
return request.results;
} catch (e) {
console.log(e);
}
}