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

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.

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



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;

      //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!
Try   "OwnerId", not "Owner"

....  where id=pp.OwnerId

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?
I figured it out.  Thank you everyone for all of your help!
Adam C.Adam C.



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