Last active
April 19, 2022 23:00
-
-
Save jackcoldrick90/a6b8efa4892e6caee53839e75fda1fdc to your computer and use it in GitHub Desktop.
This code looks at all line items associated with a deal. It then updates a multiple checkbox property for the associated company so we can store properties at a company level. * This allows us to quickly and easily view products when viewing a company record, segment companies for account based selling and use company workflows to set a "best n…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Overview: This code looks at all line items associated with a deal. It then updates a multiple checkbox property for the associated company so we can store properties at a company level. | |
* This allows us to quickly and easily view products when viewing a company record, segment companies for account based selling and use company workflows to set a "best next product". | |
* The "best next product" represents the most appropriate product to cross sell based on the current products associated with a company. | |
*/ | |
//1. Import HubSpot Client Library. | |
const hubspot = require('@hubspot/api-client'); | |
//2. Function houses our logic. | |
exports.main = async (event, callback) => { | |
//3. Define variables | |
let dealId = event.object.objectId; // store deal ID. | |
let associatedLineItems = []; // Array to store the names of associated line items. | |
let companyId, existingProducts; // Variables will be used later | |
//4. Create new instance of API client | |
const hubspotClient = new hubspot.Client({ | |
apiKey: process.env.HAPIKEY // reference HAPI key defined as secret. | |
}); | |
//5. Get the currently enrolled deals associated line items and store names in array. | |
try { | |
const apiResponse = await hubspotClient.crm.deals.associationsApi.getAll(dealId, "line_item"); // Use CRM Deals API: https://developers.hubspot.com/docs/api/crm/deals | |
// 5.1. Loop through each line item ID and get the corresponding name of the object. | |
apiResponse.body.results.forEach(item => { | |
try { | |
hubspotClient.crm.lineItems.basicApi.getById(item.id, ["name"]).then(results => { // Use CRM Line Items API: https://developers.hubspot.com/docs/api/crm/line-items | |
associatedLineItems.push(results.body.properties.name); // Store each name in array | |
}) | |
} catch (e) { | |
console.log(JSON.stringify(e.response, null, 2)); | |
} | |
}); | |
} catch (e) { | |
e.message === 'HTTP request failed' ? | |
console.error(JSON.stringify(e.response, null, 2)) : | |
console.error(e) | |
} | |
//6. Get the currently enrolled deals 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) | |
} | |
//7. Update the associated company "products" property with the line item names captured earlier. | |
let commaSeperated = associatedLineItems.join(";"); // join() returns array as string | |
try { | |
// 7.1 Use CRM Company API to retrieve current values for "product" property (if any) | |
const apiResponse = await hubspotClient.crm.companies.basicApi.getById(companyId, ["products"]) // Use CRM Company API: https://developers.hubspot.com/docs/api/crm/companies | |
existingProducts = JSON.stringify(apiResponse.body.properties.products, null, 2); // Store product property in variable | |
} catch (e) { | |
e.message === "HTTP Request Failed" ? | |
console.error(JSON.stringify(e.response, null, 2)) : | |
console.error(e) | |
} | |
const properties = { | |
"products": existingProducts.replace(/['"]+/g, '') + ";" + commaSeperated | |
} | |
const SimplePublicObjectInput = { | |
properties | |
}; | |
try { | |
const apiResponse = await hubspotClient.crm.companies.basicApi.update(companyId, SimplePublicObjectInput); // Use CRM Company API: https://developers.hubspot.com/docs/api/crm/companies | |
console.log(JSON.stringify(apiResponse.body, null, 2)); | |
} catch (e) { | |
e.message === "HTTP Request Failed" ? | |
console.error(JSON.stringify(e.response, null, 2)) : | |
console.error(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment