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
Richard Houston 20Richard Houston 20 

Update child records with lookup to user object

I'm getting my feet wet trying to write my first trigger and like many others am running into some errors. 

We have a custom object, Project Information, that is a child in a master-detial lookup to opportunities. I have a user lookup field on both opportunities and project information that I want to keep in sync with each other. Changes to the lookup only occur on the Opportunity record. 

I'm getting an error on the code below on line 4 (obj.REO_User_Record__c=Opportunity.REO__c;), "I'llegal assignemtn from Schema.SObjectField to Id. How do I get the UserID from the opportunity lookup into the Project Information lookup?

Thanks for the help!
 
trigger UpdateREO on Opportunity (after update,after insert) {
	List<Project_Information__c>IstToUpdate=new List<Project_Information__c>();
    for(Project_Information__c obj :[select id,name,REO_User_Record__c,Opportunity__c from Project_Information__c where Opportunity__c != null and Opportunity__c in : trigger.new]){
        obj.REO_User_Record__c=Opportunity.REO__c;
        IstToUpdate.add(obj);        
    }
    if(!IstToUpdate.isEmpty())
        update IstToUpdate;
    
}

 
Best Answer chosen by Richard Houston 20
Chandra Sekhar CH N VChandra Sekhar CH N V
Please ignore the above post, use the below 
obj.REO_User_Record__c=obj.Opportunity.owner; 

All Answers

Chandra Sekhar CH N VChandra Sekhar CH N V
Try  using 'Opportunity.Ownerid' at line 4  as Owner is nothing but your standard opportunity lookup to user.
Richard Houston 20Richard Houston 20
Thanks Chandra, unfortunately that didn't solve the problem warning. Any other thoughts? 
Chandra Sekhar CH N VChandra Sekhar CH N V
Try Using obj.REO_User_Record__c=Opportunity.owner; 
Chandra Sekhar CH N VChandra Sekhar CH N V
Please ignore the above post, use the below 
obj.REO_User_Record__c=obj.Opportunity.owner; 
This was selected as the best answer
Richard Houston 20Richard Houston 20
Chandra,

That was the right direction to send me in thanks!

I think this is what I need to change to make this work. 

Adding in the Obj.Opportunity__r.(Field) gave the trigger the proper field reference. Where as when I started wtih the trigger it was kind of a lost reference of different data types. 

Next I also had to change my select statement to include the Opportunity__r.REO__c field. Not having it in there threw an error on the Opportunity when I went to save. The error was an field reference that wasn't selected in the SOQL statement. 

Thanks for the help!

Final trigger:
trigger UpdateREO on Opportunity (after update,after insert) {
	List<Project_Information__c>IstToUpdate=new List<Project_Information__c>();
    for(Project_Information__c obj :[select id,name,REO_User_Record__c,Opportunity__r.REO__c from Project_Information__c where Opportunity__c != null and Opportunity__c in : trigger.new]){
        obj.REO_User_Record__c=Obj.Opportunity__r.REO__c;
        IstToUpdate.add(obj);        
    }
    if(!IstToUpdate.isEmpty())
        update IstToUpdate;
    
}

No I just need to try to write the test. 
Richard Houston 20Richard Houston 20
Thought I'd share the test class for reference. It returned 100% code coverage. 
 
@isTest
public class TestUpdateREO {
    
    @isTest static void TestUpdateREOwithoneOpportunity(){
        //test data setup
        //create an opportunity and project information then update the REO
        Opportunity opp = new Opportunity(Name ='Test Opportunity', REO__c ='005i00000018rIU',StageName = 'Pursuing', CloseDate = System.Date.Today(), Estimated_fee_Percentage__c=10  );
        insert opp;
        Project_Information__c projinfo = new Project_Information__c(opportunity__c = 'Test Opportunity',
                                                                     Estimated_Substantial_Completion__c = System.Date.today(),
                                                                     Estimated_Completion_Date__c = System.Date.today(),
                                                                     Division__c = 'Base (00100)',
                                                                     Company_Entity__c = 'Atkinson (6000)',
                                                                     Gross_Square_Footage__c = 'N/A (ZZZ)',
                                                                     Payment_Terms__c = 'Other',
                                                                     Certified_Payroll_Pickval__c = 'No',
                                                                     Construction_Type__c= 'Other');
        insert projinfo;
        
        //Update REO to run trigger
        Test.startTest();
        Opp.REO__c = '005i00000018www';
            update(opp);
    }
   
    
}