You need to sign in to do that
Don't have an account?
Collen 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:
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:selectList> <apex:inputfield value="{!Timesheet__c.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;} // 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(); } }
Thanks
Gulshan Raj
All Answers
Thanks
Gulshan Raj