I have a visualforce page with a field for the user to select the record owner. I am default the field to user because 90% of the time, the owner will be the user. But occasionally the user will change the record owner to another user, however, when we change the field to another person and click "save", the owner defaults back to the user. I assume this is a problem with my controller (get/set?). Any feedback would be appreciated.
Best, Collen
Visualforce:
<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="{!timesheet.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>
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;}
public Timesheet__c timesheet {
get {
if (timesheet == null)
timesheet = new Timesheet__c ();
timesheet.OwnerId = UserInfo.getUserId(); //Where I set the user as default
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();
}
}