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
Rolando EstevesRolando Esteves 

Opportunity Trigger

Hi! Im currently working on a trigger in order to find the xtra information of the Opportunity Owner. If Someone can help me with the part im missing from my code it would great!

 

Thanks in advance!

 

REG

 

Heres my code :

 

trigger accountManager on Opportunity (before insert) {
    
    // Create a set of userids
    set<ID> L_am = new set<ID> ();
    for(Opportunity opp: Trigger.new)
    {
       L_am.add(opp.OwnerId);
    }
    
    // Query the user table to grab the information needed
    if(!L_am.isEmpty())
    {
       User[] L_user = [SELECT Name, Email, Phone FROM USER WHERE Id IN:L_am]; 
    }
       
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Rolando EstevesRolando Esteves

Done:

 

trigger accountManager on Opportunity (before insert, before update) {
    
    // Create a set of userids
    set<ID> L_am = new set<ID> ();
    
    for(Opportunity opp: Trigger.new)
    {
       L_am.add(opp.OwnerId);
    }
    
    // Query the user table to grab the information needed
    if(!L_am.isEmpty())
    {
       User[] L_user = [SELECT Name, Email, Phone FROM USER WHERE Id IN:L_am]; 
    
       // Now create a maps of record ids to user information
       map<ID,String> M_user = new map<ID,String> ();
       map<ID,String> M_user2 = new map<ID,String> ();
       map<ID,String> M_user3 = new map<ID,String> ();
       for(User usr: L_user)
       {
          M_user.put(usr.id,usr.name);
          M_user2.put(usr.id,usr.phone);
          M_user3.put(usr.id,usr.email);
          
       }
       
       // Publish the field you want with the user information
       for(Opportunity op: Trigger.new)
       {
           op.Account_Manager_Name__c = M_user.get(op.OwnerID);
           op.Account_Manager_Email__c = M_user3.get(op.OwnerID);
           op.Account_Manager_Phone__c = M_user2.get(op.OwnerID);
       }
    }
}