You need to sign in to do that
Don't have an account?
Naofumi Kun
Trigger to update the Contact Owner depending on Email field value
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:
Thank you!
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!
If you have not created trigger then please create trigger on Contact Object.
Use below code. This code will work in case of insert and update:
Please mark as best answer if helped.
Regards,
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 still it does not work.