Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* HubSpot custom code action
*
* Capitalizes the first letter of every part of a contact's first name, last name, address and city.
*
* Can easily be configured to update any other contact property.
*
* Examples:
* - john -> John
* - DOE -> Doe
* - main-street -> Main-Street
*
*/
const hubspot = require('@hubspot/api-client');
const namesToChange = [
"firstname",
"lastname",
"address",
"city"
];
exports.main = (event, callback) => {
callback(processEvent(event));
};
function processEvent(event) {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
const contactId = event.object.objectId;
hubspotClient.crm.contacts.basicApi.getById(contactId, namesToChange)
.then(results => {
let properties = {};
namesToChange.forEach(function(element){
if(results.body.properties[element] !== null){
properties[element] = capitalizeName(results.body.properties[element]);
}
});
hubspotClient.crm.contacts.basicApi
.update(
contactId,
{
properties,
}
)
})
.catch(err => {
console.error(err);
});
}
function capitalizeName(name) {
return name
.toLowerCase()
.trim()
.replace(/\b(\w)/g, s => s.toUpperCase());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment