How to delete .invalid In user Email (For selected emails only Or for all users) for newly created sandbox or refreshed sandbox
1. Create this class
--------------------------------------------------------------------
public class EmailUtils {
public Static String removeInvalidDomain(String email) {
if (email != null && email.contains('@')) {
String[] parts = email.split('@');
String username = parts[0];
String domain = parts[1].toLowerCase();
if (domain.endsWith('.invalid')) {
domain = domain.substring(0, domain.length() - 8);
}
return username + '@' + domain;
}
return email;
}
}
2. execute this in an anonymous window:
-------------------------------------------------------------------------------------------
Set<String> eSet= new Set<String>{'pmills@bread.org'};
//above you need to provide list of emails without .invalid or we can retrieve them from prod
List<User> uList=[select id,Email from User];
List<User> upList=new List<User>();
Set<String> sSet=new Set<String>();
Integer i=0;
for(User u:uList){
String s=EmailUtils.removeInvalidDomain(u.Email);
if(u.Email !=s && s!=''){
if(eSet.contains(s)){
u.Email=s;
upList.add(u);
}
}
}
update upList;
=========================================================================
If we would like to do for all the emails in sandbox then step one is same
need to change step 2 as below.
2.
List<User> uList=[select id,Email from User];
List<User> upList=new List<User>();
Set<String> sSet=new Set<String>();
Integer i=0;
for(User u:uList){
String s=EmailUtils.removeInvalidDomain(u.Email);
if(u.Email !=s && s!=''){
u.Email=s;
upList.add(u);
}
}
update upList;
Comments
Post a Comment