Hello fellow HubsSpotters!
We have an issue with not having First and Last name values in our CRM (lazy sales reps etc). However many email-values of our prospects contains their first and last name. Eg First_name.Last_name@companydomain.com
I therefore wonder if there is a way to use the custom code function in workflows, or something of the like…
If there is, maybe I can use workflows to:
identify an email adresses that has a “value1” separated by a dot to “value2”. eg Value1.Value2@email.com
and then get Hubspot to:
Copy value1 to First name
Copy value2 to Last name
I understnad that this solution wont be perfect based on the email adress, but its better than leaving them blank. Maybe there is a way to not copuy values les than two characters etc to avoid naming people a single letter, or exclude numbers or something. All ideas would be much appreciated. Thanks ![]()
Hey, @AxelS
Happy to brainstorm with you. I think you are on the right path with using a Custom Coded Workflow Action. I don’t have a full custom coded example for you. And let’s list out the parts, assuming JavaScript is your tool of choice.
Here’s what come to my mind:
- Extract the email address
- Use Regex to find the first and last names separated by a dot
- Capitalize the first letter of the extracted names
- Run a check to ignore single-letter names or numeric values
- Outputs your shiny new first and last names to be used in the rest of your workflow
Regular expressions always make my head hurt. You might consider this one:
// Regular expression to match email in the format of first.last@example.com
const namePattern = /^([a-zA-Z]+)\.([a-zA-Z]+)@/;
const match = email.match(namePattern);
While the community likely cannot provide complete custom code for you, sharing your code and any errors you get back, gives them something specific to help troubleshoot.
Have fun building! — Jaycee
Thank you for setting me on the right path.
I have (with the help of a developer and some ChatGPT) tried this code. I don’t get any errors, but the first and last name does not populate on my contact. Since I don’t have any errors in the code I was wondering if I need to ad som extra steps in my workflow? Right now I just have the enrollment trigger and then the code action.
Thanks for any help in this mystery!
exports.main = async (event, callback) => {
const email = event.inputFields['email'];
if (email && typeof email === 'string') {
const namePattern = /^([a-zA-Z]+)\.([a-zA-Z]+)@/;
const match = email.match(namePattern);
if (match) {
const firstname = match[1];
const lastname = match[2];
callback({
outputFields: {
email: email,
firstname: firstname,
lastname: lastname
}
});
}
}
};
``
This worked a treat.. thanks for sharing.
The workflow was setup like this.
With email being input
This code:
exports.main = async (event, callback) => {
/*****
Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.
*****/
const email = event.inputFields['email'];
let firstname;
let lastname;
let emailMatch;
if (email && typeof email === 'string') {
const namePattern = /^([a-zA-Z]+)\.([a-zA-Z]+)@/;
const match = email.match(namePattern);
if (match) {
firstname = match[1].charAt(0).toUpperCase() + match[1].slice(1); //Setting and formating first letter to upper case
lastname = match[2].charAt(0).toUpperCase() + match[2].slice(1); //Setting and formating first letter to upper case
emailMatch = true;
} else{
emailMatch = false;
console.log(`Email match is ${emailMatch}`)
//throw new Error("Email doesn't match pattern");
}
}
/*****
Use the callback function to output data that can be used in later actions in your workflow.
*****/
callback({
outputFields: {
firstname: firstname,
lastname: lastname,
emailMatch: emailMatch
}
});
}
Outputs as:
Hey @sMoyse , this looks perfect!
could you show in more detail how you did the branching and set properties parts as I can’t work out how you’ve done those parts ![]()
This is really helpful, im just struggling with how you did the branching as i cant find how you set these up either
Hello @AxelS ,
I stumbled onto a similar case a few weeks ago. If you want a solution more generic, we used an LLM, integrated into a Hubspot Custom Code and it gave spectacular results ![]()
It handles easily first.last@domain.com, first@last.com, first.last.012@gmail.com, first-last@yahoo.com, it guesses most of time which one is (likely) the first name, the last name, with a really large set of languages.
Let’s get in touch if needed!
Wrote this python code instead. Takes in email and gives you first name, last name, and number of names (0, 1, 2). Branch if count is 1 or 2.
def main(event):"""Extract firstname and lastname from email, and calculate namecounts. Returns:dict: A dictionary with extracted 'firstname', 'lastname', and 'namecounts'."""email = event.get("inputFields", {}).get("email", "")firstname = Nonelastname = Nonenamecounts = 0if email and isinstance(email, str):# Split the email into local and domain partslocal_part = email.split("@")[0]parts = local_part.split(".")# Extract firstname and lastnameif len(parts) > 0 and parts[0]:firstname = parts[0].capitalize()namecounts += 1if len(parts) > 1 and parts[1]:lastname = parts[1].capitalize()namecounts += 1return {"outputFields": {"firstname": firstname,"lastname": lastname,"namecounts": namecounts}}



