Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Created April 20, 2022 09:10
Show Gist options
  • Save jackcoldrick90/84ca6eb6cd53a55635c7e6605bee7cb6 to your computer and use it in GitHub Desktop.
Save jackcoldrick90/84ca6eb6cd53a55635c7e6605bee7cb6 to your computer and use it in GitHub Desktop.
This code takes the most recently enrolled deal and using the associated company ID obtains the close amount of the most recently closed deal. It then passes this data back to the workflow which is copied toa property value and we can use this to calculate the upsell value.
/*
* Overview: This code takes the most recently enrolled deal and using the associated company ID obtains the close amount of the most recently closed deal.
* It then passes this data back to the workflow which is copied toa property value and we can use this to calculate the upsell value.
*/
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
let companyId, recentDealClosedAmount; // Variables will be used later
let dealId = event.object.objectId; // store deal ID.
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
//1. Get associated company ID
try {
const apiResponse = await hubspotClient.crm.deals.associationsApi.getAll(dealId, "company"); // Use CRM Company API: https://developers.hubspot.com/docs/api/crm/companies
companyId = apiResponse.body.results[0].id; // Store ID in variable
} catch (e) {
e.message === 'HTTP request failed' ?
console.error(JSON.stringify(e.response, null, 2)) :
console.error(e)
}
//2. Get recently closed deal associated to company
const filterGroup = { //FILTERS
filters: [{
propertyName: 'associations.company',
operator: 'EQ',
value: companyId
}]
}
const sorts = [{ //SORT
propertyName: 'closedate',
direction: 'DESCENDING'
}]
const properties = ['amount', 'dealname'] //PROPERTIES
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
sorts,
properties
}
hubspotClient.crm.deals.searchApi.doSearch(publicObjectSearchRequest).then(results => {
console.log(JSON.stringify(results.body))
recentDealClosedAmount = results.body.results[1].properties.amount;
console.log(recentDealClosedAmount);
// 3. Pass back to workflow
callback({
outputFields: {
recentDealClosedAmount: recentDealClosedAmount
}
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment