That should do the trick.
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.SECRET_NAME // Change "SECRET_NAME" with your Private App key secret name
});
let email;
try {
const ApiResponse = await hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["email"]);
email = ApiResponse.properties.email;
let names = email.split("@"); // split emaill adress by "@"
let fnln = names[0].split("."); // get whatever is before "@" and plit it by "."
var fn = fnln[0]; //get whatever is before "." (First name)
var ln = fnln[1]; // get whatever is after "." (Last name)
if(fnln.length == 2 ){ // checking if First Name and Last Name exists together and are separeted by "."
hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
"properties": {
"firstname": fn, // update contact record with First Name
"lastname": ln // update contact record with Last Name
}
});
} else{
// WIll update First Name as John and Last Name as Doe
hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
"properties": {
"firstname": "John",
"lastname": "Doe"
}
});
}
} catch (err) {
console.error(err);
// We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
throw err;
}
}