Created
December 7, 2022 17:52
-
-
Save NickDeckerDevs/285346d02b0021e2f633fa939cccae95 to your computer and use it in GitHub Desktop.
workflow custom code in hubspot
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
const hubspot = require('@hubspot/api-client'); | |
// adding async to this main function here | |
exports.main = async (event, callback) => { | |
// Instantiate a new HubSpot private app (secret) | |
const hubspotClient = new hubspot.Client({ | |
accessToken: process.env.custom_code_private_app | |
}); | |
// set up a try catch statment. Only one is needed, so if there is an error in any of this we can exit out and report on it | |
try { | |
// adding await here, because we have async at the top in the main function, then we can tell the program to | |
// wait for this repsonse before going further (similar to the then() but a bit cleaner in my opinion) | |
const dealResponse = await hubspotClient.crm.deals.basicApi.getById(event.object.objectId, ["agent_email"]) | |
// because we aren't changing the value of email later down the road, we just create a const | |
const agentEmailValue = dealResponse.body.properties.agent_email; | |
const filter = { propertyName: 'email', operator: 'EQ', value: agentEmailValue } | |
const filterGroup = { filters: [filter] } | |
const sort = JSON.stringify({ propertyName: 'email', direction: 'DESCENDING' }) | |
const properties = ['email'] | |
const limit = 1 | |
const after = 0 | |
const searchCriteria = { | |
filterGroups: [filterGroup], | |
sorts: [sort], | |
properties, | |
limit, | |
after | |
} | |
// lets tell the code to await this response as well | |
const searchContactResponse = await hubspotClient.crm.contacts.searchApi.doSearch(searchCriteria) | |
if (searchContactResponse.body.total == 0) { | |
console.log("Contact WITH Email " + agentEmailValue + "NOT FOUND.") // - FOR DEBUG | |
// I like to set up an output for error as string, that way I can create lists or something or do if statements on the workflow to do other things | |
// like internal emails, etc -- when you review the details of this in workflows it will show this as output, makes it easier then testing | |
// individual items inside the code and makes life a bit simplier | |
callback({ | |
outputFields: { | |
errorMessage: "Contact WITH Email " + agentEmailValue + "NOT FOUND." | |
} | |
}); | |
} | |
// because we exit them out of here bceause they aren't found, we don't need to have an else statement | |
const updateAssociations = await hubspotClient.crm.contacts.associationsApi.create(searchContactResponse.body.results[0].id, 'deals', event.object.objectId, 'agent'); | |
// you can even have a success output here as well, it is a bit redundent, because if you get an error | |
// you will catch it, make an output for UpdatedAssociation as a boolean | |
callback({ | |
outputFields: { | |
updatedAssociation: true | |
} | |
}); | |
} catch (err) { | |
console.error(err); | |
callback({ | |
outputFields: { | |
errorMessage: err | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment