Skip to content

Instantly share code, notes, and snippets.

@alewolf
Last active May 29, 2021 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alewolf/f83a5405bd13e13abde210d03fde819f to your computer and use it in GitHub Desktop.
Save alewolf/f83a5405bd13e13abde210d03fde819f to your computer and use it in GitHub Desktop.
/**
* 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