Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertainslie/12314f50a8d2073f64a6ebef65953447 to your computer and use it in GitHub Desktop.
Save robertainslie/12314f50a8d2073f64a6ebef65953447 to your computer and use it in GitHub Desktop.
HubSpot Operations Hub - Count High Value Line Items.
const hubspot = require("@hubspot/api-client");
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.YOUR_ACCESS_TOKEN,
});
//First, make a call to get deal associations
hubspotClient.crm.deals.basicApi
.getById(event.object.objectId, [], [], ["line_items"])
.then((results) => {
//Because a separate api call is needed for each associated line item, and each one returns a promise
//where the promise is not dependent on any other call, each can be run using the Promise.all() method
//The map() method takes 2 arguments - an initial array and a function to apply to every item in the initial array
// then, returns an array of the results. in this case, it's an array of promises that each get line item details
let lineItemPromises = results.associations["line items"].results.map(
(item) => {
return hubspotClient.crm.lineItems.basicApi
.getById(item.id, ["amount"])
.then((results) => {
console.log(results);
return results;
});
}
);
//pass the array of promises into Promise.All, which then returns an array of all results, in this case the details
// of every line item api call request.
Promise.all(lineItemPromises).then((resultsArray) => {
//console.log(resultsArray)
//use the .filter() method to filter the array of results for just the high value line items
let highValueLineItems = resultsArray.filter(
(item) => parseFloat(item.properties.amount) > 1000.0
);
console.log(`Length: ${highValueLineItems.length}`);
//update the original deal with the newly calculated property value
hubspotClient.crm.deals.basicApi
.update(event.object.objectId, {
properties: {
number_high_value_line_items:
highValueLineItems.length.toString(),
},
})
.then((response) => {
console.log(response.body);
callback({
outputFields: {
highValueLineItems: highValueLineItems.length,
},
});
});
});
})
.catch((err) => {
console.error(err);
});
};

This code snippet is illustrative of a common pattern in CRM - getting associated records. This is particularly useful for deals and line items where line items are inherently tied to a specific deal.

This particularly code snippet shows this in context of the following use case: A customer is looking to more easily quantify how many high value items are included on a deal. For every Closed Won deal, they would like to set a property value of the number of line items greater than $1,000 (Note: You can build a similar report like this in the custom report builder...but assume the customer wants to include the value as a deal property)

@rjreiffer
Copy link

Robert, being very new to this, how hard would it be to basically sum up the Monthly Recurring Revenue line items (hs_mrr) and IF the amount = 0 then replace amount with the total of hs_mrr line items?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment