Get Name from Email in Apex

Ever had an email like john_doe@example.com and wondered if there’s a way to get the person’s name from it? 🤔

Well, you’re in luck! In this blog post, we’ll write a simple Apex method that does exactly that: it takes an email address as input, extracts the part before the @ symbol, and returns a neatly formatted full name. This is a neat trick, especially useful for automation, lead enrichment, or just making your user data a bit more readable!

Let’s dive in. 💡

🧠 What Are We Trying to Do?

Given an email like:

john_doe@example.com

 

We want to:

  • Extract john_doe from the email

  • Replace the underscore _ with a space

  • Return: john doe

If the email doesn’t follow this pattern, the method will return an empty string or partial data.

🔍 Code Explanation

 
public class ApexUseCaseThirteen {
    
    public static String generateFirstAndLastName(String emailAddress){

We start by defining a class called ApexUseCaseThirteen, with a static method named generateFirstAndLastName. It takes one input: emailAddress.

        String fullName = '';
        String extractedString = emailAddress.substringBefore('@');

We initialize a fullName string to store the final result. Then, we use substringBefore('@') to get everything before the @ symbol this is usually where the person’s name is.

        if(String.isNotBlank(extractedString)){
            if(extractedString.contains('_')){
                fullName = extractedString.replace('_', ' ');
            }
        }

We check:

  • Is the extracted string not blank?

  • Does it contain an underscore (_)?

If yes, we replace the _ with a space using replace() to make it look like a real name.

        return fullName;
    }
}

Finally, we return the formatted full name (or an empty string if no name was extracted).

✅ Why This Is Useful

 

This method is simple, but super helpful in many real-world scenarios:

  • Auto-generating names from email signups

  • Filling missing data for leads or contacts

  • Improving personalization in automated messages

It’s also a great way to practice using:

  • substringBefore

  • String.replace

  • String.isNotBlank

  • Conditional logic in Apex

Want to make it better? You can extend this method to:

  • Capitalize the first letter of each name

  • Handle dots (.) or hyphens (-) as well

  • Use regex for more advanced formatting

🧾 Final Code Snippet

 
public class ApexUseCaseThirteen {
    public static String generateFirstAndLastName(String emailAddress){

        String fullName = '';
        String extractedString = emailAddress.substringBefore('@');
        if(String.isNotBlank(extractedString)){
            if(extractedString.contains('_')){
                fullName = extractedString.replace('_', ' ');
            }
        }
        return fullName;
    }
}

🎥 Watch It in Action!

Want to see how this works inside Salesforce? Watch my short demo where I show you how the method processes different email formats and gives clean, readable names.

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)