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
Collen Mayer 6Collen Mayer 6 

Record Owner field not working on Visualforce Page

Greetings all,
I have a visualforce page with an apex:inputfield for ownerid for the record to create a timesheet.  I am currently defaulting the field to the current user (the current user will usually be creating a timesheet for themselves), but would like the ability to change the value in the owner field if they choose.  Currently the field is defaulting correctly, but if the user changes from the default user to another user, the value doesn't save (it goes back to the default value/current user).  I assume some problem with my controller.  Please advise. 

Thanks,
Collen

My code is below.
 
<apex:page standardController="Timesheet__c" extensions="PayPeriodExtension">
   
    <apex:form >
            
    <apex:pageBlock title="Select Pay Period">
      <apex:pageMessages /> <!-- this is where the error messages will appear -->

        <apex:pageBlockSection >
                
       	<apex:selectList size="1" required="true" value="{!PayPeriodID}">
          <apex:selectOptions value="{!ActivePayperiods}"></apex:selectOptions>
      	</apex:selectList>
        <apex:inputfield value="{!OwnerID}"/>	<br/>

        </apex:pageBlockSection>
            <apex:pageBlockButtons >
            <apex:commandButton action="{! save }" value="Save" />        
        	<apex:commandButton action="{! cancel }" value="Cancel" />        
        </apex:pageBlockButtons>      
    </apex:pageBlock>
 </apex:form>
           
</apex:page>

Extension:
 
public class PayPeriodExtension {
    public ApexPages.StandardController stdCntrlr {get; set;}
    Public List  <Pay_Period__c> PPTemp = new List <Pay_Period__c>(); 
    Public String PayPeriodID {get; set;}
   
    // the contact record you are adding values to
  public Timesheet__c timesheet {
    get {
      if (timesheet == null)
        timesheet = new Timesheet__c ();
        timesheet.OwnerId = UserInfo.getUserId();
        return timesheet;
    }
    set;
  }
	public  PayPeriodExtension(ApexPages.StandardController controller) {
        stdCntrlr = controller;    
  }

   
    public List<SelectOption> ActivePayperiods
    {
        get
        {
            PPTemp = [Select Id, Name, Days_Since_Start_Date__c From Pay_Period__c 
                      Where (Days_Since_Start_Date__c <14 AND days_Since_Start_Date__c >-21) 
                      Order BY Days_Since_Start_Date__c desc ];
            ActivePayPeriods = new List<SelectOption>();
            
            for(Pay_Period__c temp : PPTemp)
            {
                ActivePayPeriods.add(new SelectOption(temp.id, temp.Name));
            }
            return ActivePayPeriods;
        }
        set;
    }
    
    public PageReference save() {
		
    try {
        timesheet.Pay_Period__c = PayPeriodID;
        upsert timesheet; // inserts the new record into the database
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new timesheet.'));
      return null;
    }

    // if successfully inserted new survey, then displays the thank you page.
    return (new ApexPages.StandardController(timesheet)).view();
  }

}