• Michael DS 6
  • NEWBIE
  • 5 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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) {
            }
        }
}