Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Created April 26, 2022 22:06
Show Gist options
  • Save jackcoldrick90/5e6e986c0c8017c43fc109c2a743a3bd to your computer and use it in GitHub Desktop.
Save jackcoldrick90/5e6e986c0c8017c43fc109c2a743a3bd to your computer and use it in GitHub Desktop.
There may be times where you'd like to assign ownership of a record using the likes of zip code/postal code. Often in situations like these there are hundreds if not thousands of values. The code snippets below show how you can solve for this using Operations Hub Professional.
/*
* This code snippet first gets a reference to the contacts postal code. Then it checks a series of arrays for the value. Each
* array represents a reps territory or region. When a match is found we assign the HubSpot Owner ID value to a variable and
* pass this back to the workflow using the callback object. You are then able to copy this value into the "HubSpot Owner"
* property.
/
exports.main = async (event, callback) => {
// Store contact postal code in variable
const zip = event.inputFields['zip'];
let owner; // will store the user ID of the owner
// Create arrays for each rep to hold assigned postal codes
const rep1 = ["1", "2", "3", "4"];
const rep2 = ["5", "6", "7", "8"];
const rep3 = ["9", "10", "11", "12"];
// Check which rep the postal code is assigned to
if (rep1.includes(zip)) { // Rep 1
console.log("Assign rep 1");
owner = "246804692";
} else if (rep2.includes(zip)) { // Rep 2
console.log("Assign rep 2");
owner = "321450460";
} else if (rep3.includes(zip)) { // Rep 3
console.log("Assign rep 3");
owner = "321450461";
} else { // If no match assign Rep 4
console.log("Assign rep 4");
owner = "321450462"
}
// Pass the data back to the workflow so we can copy to owner property
callback({
outputFields: {
"owner": owner
}
})
}
/*
* This code snippet is similar to the above but instead rather than using the callback object we use the CRM API to update
* the "HubSpot Owner" property. This code snippet first gets a reference to the contacts postal code. Then it checks a series of
* arrays for the value. Each array represents a reps territory or region. When a match is found we store the HubSpot Owner ID value
* to a variable and call the HubSpot Contact CRM API to update the "hubspot_owner_id" property.
/
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
// Store contact postal code in variable
const zip = event.inputFields['zip'];
let owner, properties;
// Create arrays for each rep to hold assigned postal codes
const rep1 = ["1", "2", "3", "4"];
const rep2 = ["5", "6", "7", "8"];
const rep3 = ["9", "10", "11", "12"];
// Create HubSpot Client
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
// Check which rep the postal code is assigned to
if (rep1.includes(zip)) { // Rep 1
console.log("Assign rep 1");
owner = "246804692";
properties = {
"hubspot_owner_id": owner
}
} else if (rep2.includes(zip)) { // Rep 2
console.log("Assign rep 2");
owner = "321450460";
properties = {
"hubspot_owner_id": owner
}
} else if (rep3.includes(zip)) { // Rep 3
console.log("Assign rep 3");
owner = "321450461";
properties = {
"hubspot_owner_id": owner
}
} else { // If no match assign Rep 4
console.log("Assign rep 4");
owner = "321450462"
properties = {
"hubspot_owner_id": owner
}
}
// Assign owner by updating "HubSpot Owner" property using CRM API
// https://developers.hubspot.com/docs/api/crm/contacts
const SimplePublicObjectInput = {
properties
}
try {
const apiResponse = await hubspotClient.crm.contacts.basicApi.update(event.object.objectId, SimplePublicObjectInput);
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