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
mayrich1mayrich1 

Copy data from one Object to another Object using Lookup Field

I am trying to create a trigger and class using Apex that will take data from a couple fields on the user record and copy them into fields on an Opportunity using the Opportunity Owner lookup field.  From within the opportunity I need to lookup the opportunity owner's user record and pull over the Title, Department, and Email from the user record and put it in the opportunity record.  I appreciate the help.
EPSWDEVEPSWDEV

you code would be something like this
Code:
trigger youtrigname on Opportunity (before insert){
 for( Opportunity opp : Trigger.new){
      
      1. get user info  user = [select yourstuff from usertable where id=:opp.Owner];
      2. set your data
           opp.email = user.email
           opp.dept = user.dept
 }
}
 remember you dont need to save since the trigger is before insert..so you just need to set the data.

HTH


 

mayrich1mayrich1
Thank you for the response.  I have updated the code but now I am getting an error message that I am not sure how to handle.  The error message is "Invalid bind expression type of SOBJECT:User for column of type Id".  Do you know why this error is occurring?  I have attached my code below:

trigger OpportunityOwnersManagerEmail on Opportunity (before insert, before update) {

    String userMgr;

     for(Opportunity opp:Trigger.new){
      //get user info 
      userMgr = [select Manager_s_Email__c from User where id=:opp.Owner];
      //set the data
      opp.Manager_s_Email__c = userMgr.Manager_s_Email__c;

     }
}

Thank you for your help!
SteveBowerSteveBower
Try   "OwnerId", not "Owner"

....  where id=pp.OwnerId

mayrich1mayrich1
Thank you for the quick response.  I made the change and that was the problem.  However, now I am getting a different error.  It is: Illegal assignment from LIST:SOBJECT:User to String.  I thought that the value could be set as a string since the field I am looking for is a text field (email).  How else should I be declaring this variable?
mayrich1mayrich1
I figured it out.  Thank you everyone for all of your help!
Adam C.Adam C.

@Mayrich1  

 

I know it has been a while since this post but I was wondering if you could post what you figured out?

 

I'm getting the same error message.

 

thank you