Last active
May 29, 2021 09:00
-
-
Save alewolf/f83a5405bd13e13abde210d03fde819f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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