+ Start a Discussion
SeanCenoSeanCeno 

String Method to extract everything to the right of '@' from Email field

All,

I'm trying to populate the Account Website field based on a child Contact's Email field. I need to grab the characters that occur after (to the right of) the '@' character. What is the proper String Method to use here?
a.Website = String.valueOf(con.Email.Split('@')); //Incorrect String Method
Below is the full trigger I've written so far:

Class:
public class Update_AccountWebsite {
    protected final Contact[] contactNewList;
    protected final Contact[] contactOldList;
    
    public Update_AccountWebsite (Contact[] contactOldList, Contact[] contactNewList) {
        this.contactNewList = contactNewList;
        this.contactOldList = contactOldList;
    }
    
    public void executeUpdate_AccountWebsite() {
        List<Contact> contList = new List<Contact>(contactNewList);
        Set<Id> accountIds = new Set<Id>();
        for(Contact c :contactNewList){
            if(c.id != null && c.Email != null){
                accountIds.add(c.AccountId);
            }
        }
        List<Account> acctList = [Select Id, Name, Website, (Select Id, AccountId, Email From Contacts) From Account Where Id in :accountIds];
        for(Account a :acctList){
            for(Contact con :a.Contacts){
                a.Website = String.valueOf(con.Email.Split('@')); //Incorrect String Method
                a.Contacts.add(con);
                system.debug(acctList);
                system.debug(contList);
                system.debug(a.Contacts);
            }
        }
    }
}

Trigger:
trigger MasterContactTrigger on Contact (
    before insert, after insert, 
    before update, after update, 
    before delete, after delete) {
        Contact[] contactOldList = trigger.IsDelete ? null : trigger.old;
        Contact[] contactNewList = trigger.IsDelete ? trigger.old : trigger.new;
        
        if (Trigger.isBefore) {
            if (Trigger.isInsert) {
            }
            if (Trigger.isUpdate) {
            }
            if (Trigger.isDelete) {
            }
        }
        
        if (Trigger.IsAfter) {
            if (Trigger.isInsert) {
                new Update_AccountWebsite(contactOldList, contactNewList).executeUpdate_AccountWebsite();
            }
            if (Trigger.isUpdate) {
                new Update_AccountWebsite(contactOldList, contactNewList).executeUpdate_AccountWebsite();
            }
            if (Trigger.isDelete) {
            }
        }
}

 
Best Answer chosen by SeanCeno
Amit Singh 1Amit Singh 1
Hi Sean,

You can use String class method called subStringAfter like below:
 
a.Website = String.valueOf(con.Email).SubStringAfter('@');
Also, refer below link to know more about String class method.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm
Let me know if this helps :)
Thanks!
Amit Singh

All Answers

AjazAjaz
Hi Sean,

Please try below code.

a.website = con.email.split('@')[1];

if above doesnt work, try below,

a.website = String.valueof(con.email).split('@')[1];

This should help you. Give a thumbsup if it helps you.

Regards,
Zaja.
Amit Singh 1Amit Singh 1
Hi Sean,

You can use String class method called subStringAfter like below:
 
a.Website = String.valueOf(con.Email).SubStringAfter('@');
Also, refer below link to know more about String class method.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm
Let me know if this helps :)
Thanks!
Amit Singh
This was selected as the best answer
Michael DS 6Michael DS 6
Split method will return a list/array.
a.Website = con.Email.Split('@')[1];