+ Start a Discussion
Collen Mayer 6Collen Mayer 6 

Display value in Owner ID field on visualforce page load

Greetings all,
I have a visualforce page with an apex:inputfield for ownerid for the record to create a timesheet.  I would like to default the record owner name on the page to the current user (the current user will usually be creating a timesheet for themselves), but give them the ability to change the value in the owner field if they choose.  Currently the field is coming up blank, rather than with the current user.  Please advise.  My code is below.

I'm still pretty new to apex so any help is greatly appreciated. 

My VF page:
<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:inputfield value="{!Timesheet__c.ownerid}"/>	<br/>

            <apex:pageBlockButtons >
            <apex:commandButton action="{! save }" value="Save" />        
        	<apex:commandButton action="{! cancel }" value="Cancel" />        
My Controller 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;
	public  PayPeriodExtension(ApexPages.StandardController controller) {
        stdCntrlr = controller;    

    public List<SelectOption> ActivePayperiods
            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;
    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();


Best Answer chosen by Collen Mayer 6
Please change Timesheet__c.ownerid to timesheet.ownerid at line 13 of VF page.

Gulshan Raj

All Answers

Please change Timesheet__c.ownerid to timesheet.ownerid at line 13 of VF page.

Gulshan Raj
This was selected as the best answer
Collen Mayer 6Collen Mayer 6
Thanks, Gulshan.  It's defaulting correctly, now.  However, if I change the value to a different owner (different than the user) after I save the visualforce page, the owner field goes back to the user in the actual record.  I assume its a problem in my extension?