I'm practicing on creating a Trigger (still many things to learn...)
I would like to know on why am I getting a blank value on Text__c, the record's email address is an email address of an exisiting User.
See code snippet below.
trigger AutoChangeOwner on Contact (before insert, before update) {
Set<String> conEmail = new set<String>();
Map<String, Id> UsrMap = new map<String, Id>();
for(Contact con : trigger.new ){
conEmail.add(con.Email);
system.debug('conemail' + conEmail);
}
List<User> userEmail = [SELECT Id, Name, Email FROM User WHERE Email != null];
for(User u : userEmail){
if(conemail.contains(u.email)){
UsrMap.put(u.Email, u.Id);
system.debug('useremail' + userEmail);
}
}
for(Contact c : trigger.new){
c.Text__c = UsrMap.get(c.Email);
}
}
Any kind of explanation or ways to enhance the code is much appreciated!