Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
I'm new to coding and I'm practicing by creating a trigger that will update the Contact Owner depending on the Email field value.

For example, I have the Email field value is test@gmail.com then it will try to look for User that has test@gmail.com email then it will update the Contact Owner.

This is what I've done so far:

 

Set<String> conEmail = new set<String>();

List<String> UsrId = new List<String>();

Map<String, Id> UsrMap = new map<String, Id>();

for(Contact con : trigger.new ){

if(con.Email != null){

conEmail.add(con.Email);

}

}

List<User> userEmail = [SELECT Id, Name, Email FROM User WHERE Email In: conEmail];

for(User u : userEmail){

UsrMap.put(u.Email, u.Id);

}

for(Contact c : trigger.new){

if(UsrMap.containsKey(c.Email)){

c.OwnerId = UsrMap.get(c.Email);

}

}

Any ideas on why Owner is not being updated when I edit any contact record.

Thank you!
2 answers
  1. Oct 29, 2019, 3:43 AM
    Hi Ramakant,

    I've already created a contact trigger and used before insert and before update.

    I tried updating the any fields from contact but the Owner field is not being updated.

    I also tried to remove 

    if(UsrMap.containsKey(c.Email))

    still it does not work.

Loading
0/9000