title: How to populate lookup field when creating a new custom object in SFDC
body: <p>I'm struggling with this situation.. I have a Standard Object in SFDC (Opportunity) that has a custom look up field pointing to the User object what I'm trying to do is populate this field with the name of the user that creates a custom object that is available in the Opportunity layout... </p>
  
  <p>i.e.  New GOP Checklist --- Then choose the type of checklist---  and then fill all the required fields and click save, this is pointing back to the Opportunity view.  To start with is this something doable ? i know that look up fields can be tricky. 
  and my second question is what's the best way to do this Programatically (trigger) or using the workflow and field update functionality ? </p>
  
  <p>Thanks !! </p>
  
  <pre><code>trigger TR_OrderChecklist on Order_Checklist__c (before insert) {
  
  //----------------------------------------------------------------------------------
  // Function 1: Update COS Operations Attribute in Opportunity
  //----------------------------------------------------------------------------------
  
  for(Order_Checklist__c o : trigger.new){
    if(o.Opportunity__r.CARE_Operations__c == null) {
      o.Opportunity__r.CARE_Operations__c = UserInfo.getUserId();
    }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is what they came up with. In the Standard Opportunity Object we have a lookup field tied to the user.. CARE_Operations__c.. Now what the trigger is supposed to do is the following..   </p>
  
  <p>1.- When creating a new GOP Checklist if the user populate a new custom lookup field in the GOP object named COSOperations_<em>c then keep that name, 
  2.- If the User didn't populate the COSOperations</em>_c field but the field in the Opp level CARE_Operations__c is populated use that name.
  3.- If neither CARE_Operations_<em>c or COSOperations</em>_c are populated (user input) then COSOperations__c is going to be the person that just create the GOP Object.</p>
  
  <p>This is what i have so far.. </p>
  
  <pre><code>trigger TR_OrderChecklist on Order_Checklist__c (before insert) {
  List&lt;Opportunity&gt; COS_Op = new List&lt;Opportunity&gt;();
  COS_Op = [select CARE_Operations__c from Opportunity where id in (select   Opportunity__c from Order_Checklist__c where COSOperations__c != null)];
  for(Order_Checklist__c OC : trigger.new) {
      if(OC.COSOperations__c != null) {
         break;}
      if(COS_Op != null){
         OC.COSOperations__c = OC.Opportunity__r.CARE_Operations__c;} 
      if(OC.COSOperations__c == null){
         OC.COSOperations__c = UserInfo.getUserId();}
  }       
  } 
  </code></pre>
  
  <p>My problem is in the second if statement.. the other 2 conditions are working properly.. ! Any ideas ? Thanks !!!</p>