function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kiran Kumar PunuruKiran Kumar Punuru 

How to update owner using email field

Hi All,

I have a requirement where i need to update the owner for the new record created to the user which has the email in the same record.

For example:

If i create a record with name : abc
                                     email : xyz@test.com

then after record was created the owner for the new record has to be changed to user associated with email xyz@test.com.

Any suggestions or ideas are helpful .

Thanks
Raj VakatiRaj Vakati
Try this code you need ot create a trigger
 
trigger UpdateOwnerID on Account(before insert, before update) {
     Set<String> setEmails = new Set<String>();
      for(Account acc : trigger.new)
      {
              setEmails.add(acc.Email);
       }
    map<string,id> userIdbyAlias = new map<string,id>();  
    for(User u : [Select id,Name from user where Email IN :setEmails])
    {
            userIdbyAlias.put(u.Email,u.Id);
     }
    for (Account accList : Trigger.new)
    {
              
      accList.OwnerId = userIdByAlias.get(accList.Email);
             
         
    }

}