⚙ Operations Hub

MSaurat
Participant

Custom Code - Get firstname & lastname from email address

Hi, 

 

I'm not a developper of any sort but I'm trying to understand custom code for worflows. I'd like to be able to get the firstname & lastname of a contact when the email address looks like this "firstame.lastname@domain.com". 

I've used this python code: 

import re

def extract_name_from_hubspot_email(email):
# Define a regular expression pattern to match HubSpot email format
pattern = r'^([a-zA-Z]+)\.([a-zA-Z]+)@.*$'

# Use re.match to extract the first name and last name
match = re.match(pattern, email)

if match:
first_name = match.group(1)
last_name = match.group(2)
return first_name, last_name

# If the email doesn't match the expected HubSpot format, return None
return None, None

# Example usage:
email = "john.doe@hubspot.com"
first_name, last_name = extract_name_from_hubspot_email(email)
if first_name and last_name:
print("First Name:", first_name)
print("Last Name:", last_name)
else:
print("Unable to extract first name and last name from the email.")

 

But not really sure how to do it with the custom code. 

 

Any help appreciated. 


Thank you 

0 Upvotes
4 Replies 4
Maciek-Magic
Member

Custom Code - Get firstname & lastname from email address

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;
  }
}

  

0 Upvotes
DanielJeal
Participant

Custom Code - Get firstname & lastname from email address

What do you want to do with the name when you get it? Update the Contacts name in HubSpot? Also, what if their email is lastname.firstname?!

0 Upvotes
MSaurat
Participant

Custom Code - Get firstname & lastname from email address

Hey Dan, 

Yes basically extract FN & LN and add the corresponding fields. I'm not worried about the email address having LN.FN@ as we're dealing with professional accounts and 99% are FN.LN@ 

0 Upvotes
MiaSrebrnjak
Community Manager
Community Manager

Custom Code - Get firstname & lastname from email address

Hi @MSaurat

 

Thank you for reaching out to the Community! 

I found this code on GitHub which could be helpful: @coldrickjack shared how to split the full name provided by a contact using a custom coded workflow action. 

@coldrickjack do you think @MSaurat could do something similar with the first & lastname of a contact's email address? Thank you!

 

Cheers
Mia, Community Team

 


Wusstest du, dass es auch eine DACH-Community gibt?
Nimm an regionalen Unterhaltungen teil, indem du deine Spracheinstellungen änderst


Did you know that the Community is available in other languages?
Join regional conversations by
changing your language settings