You need to sign in to do that
Don't have an account?
SeanCeno
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?
Class:
Trigger:
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 MethodBelow 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) { } } }
You can use String class method called subStringAfter like below:
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
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.
You can use String class method called subStringAfter like below:
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