|
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); |
|
}); |
|
}; |
Hi
So i did tweak the start bit, as being a complete novice to JS i didn't fully understand so pinched a separate bit of code from some of the API documentation
I'm essentially trying to do similar to what you have achieved but for companies, find how many associated contacts it has and update a total_contacts property
I did have one of our devs look at it who thought it may have been the '>=' so some of the code is tweaked slightly (maybe that's where i went wrong!) and i also got an error from lines 16 on your original as it said 'results' is already defined above
I also obviously have our actualy API key where it says ourkey but have removed for now
Anyway here is my code, appreciate it slightly different user case so no worries if can't help but appreciate you taking the time anyway
`var hubspot = require('@hubspot/api-client');
exports.main = function (event, callback) {
var hubspotClient = new
hubspot.Client({"apiKey":"ourkey"});
}`