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?

marked as duplicate by Santanu Boral, Raul, Ratan Paul, Brian Mansfield, Ashwani May 16 '17 at 19:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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; }
  • 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 May 15 '17 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 May 15 '17 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 May 15 '17 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 May 15 '17 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 or ask your own question.