1

I have read through all the posts really but still not able to figure this out. I want to pass the no. of hours that are input on the VF Page back to the controller. Basically, user should be able to update the no. of hours on his saved Timesheet.
Below is my VF Page -

<apex:page standardController="Project_Timesheet__c" extensions="i3_ProjectTimesheet_Controller_Edit" sidebar="false">
<apex:inputText value="{!TSHours}" label="Hours" id="TSHours"/> 

Below is my extension

public class i3_ProjectTimesheet_Controller_Edit {

public i3_ProjectTimesheet_Controller_Edit(ApexPages.StandardController stdController) 
{
        CurrentTS = (Project_Timesheet__c)stdController.getRecord();
        CurrRecId = ApexPages.currentPage().getParameters().get('Id');
TSRecord = [Select Contract__c, Assigned_To__c, Account__c, Project__c, ProjectPhase__c, Hours__c, Comments__c, Date__c from Project_Timesheet__c where ID = :CurrRecId];
}
public decimal TSHours 
{
  get {
        return TSRecord[0].Hours__c;  
      }

  set;
}
public PageReference Save()
{
  system.debug (TSHours);
}

What am I missing?

0

2 Answers 2

4

The getter is returning the original value that was queried. Just use a normal getter, and assign the default value in your constructor.

public i3_ProjectTimesheet_Controller_Edit(ApexPages.StandardController stdController) 
{
        CurrentTS = (Project_Timesheet__c)stdController.getRecord();
        CurrRecId = ApexPages.currentPage().getParameters().get('Id');
TSRecord = [Select Contract__c, Assigned_To__c, Account__c, Project__c, ProjectPhase__c, Hours__c, Comments__c, Date__c from Project_Timesheet__c where ID = :CurrRecId];
        TSHours = TSRecord[0].Hours__c; 
}
public decimal TSHours { get;  set; }
4
  • Slight update to sfdc fox , if you want just write a null check and return your default TSHours like , if(TSHours!=null) return TSHours; return TSRecord[0].Hours__c
    – freaker87
    Commented May 15, 2017 at 18:47
  • Thank You so much! I knew there was something different with my getter/setter method but couldn't figure it out. It worked.
    – Maneesh
    Commented May 15, 2017 at 18:49
  • @freaker87 That isn't the same as providing a default; if the user clears out the field, the value will revert to the queried value. This may or may not be the desired outcome, but could be useful to know about.
    – sfdcfox
    Commented May 15, 2017 at 18:50
  • @sfdcfox you are correct, this may not be a desired outcome , but just exposing him to one more view on the same thread :-)
    – freaker87
    Commented May 15, 2017 at 18:53
0

An alternative if you want to assign directly to your record's Hours__c field is to use a custom setter:

public decimal TSHours 
{
  get {
        return TSRecord[0].Hours__c;  
      }

  set {
        TSRecord[0].Hours__c = value;
      }
}

Not the answer you're looking for? Browse other questions tagged .