3

I have a visual force page where I'm using an action method to mark a "timeStamp" of when the user opens the page for a given object.

Page:

apex:page standardController="myChart" extensions="chartExtension" action="{!markTime}"> ...

Extension code:

public void markTime() {
      myChart__c c = [Select Id, myTimestamp__c From myChart__c Where Id = :chartVariable.Id];
        if(c.myTimestamp__c == null) {
            c.myTimestamp__c = DateTime.now();
            update c;
        }

This works well enough in that it updates the database correctly, but the field is still blank on the page. So far nothing I've tried has worked, is there a simple way to do this?

1 Answer 1

2

Since you're using an extension, you can use the StandardController object, passed in through the constructor, call getRecord() and modify the record. See the code below for an example. Note, since I'm updating myTimestamp in the constructor, there's probably no need for the "markTime" action.

Apex:

public class ChartExtension{
    public ChartExtension(ApexPages.StandardController ctrl){
        MyChart__c m = (MyChart__c)ctrl.getRecord();
        m.myTimestamp__c = Datetime.now();
    }
}

Visualforce

<apex:page standardController="MyChart__c" extensions="ChartExtension">
    <apex:form >
        <apex:inputField value="{!MyChart__c.myTimestamp__c}" />
    </apex:form>
</apex:page>
1
  • As an addendum, the original code fails because the record that is on the page has already been queried by the time markTime() is called, so any changes to the record via DML won't appear until the next time the page reloads.
    – sfdcfox
    Commented Feb 16, 2014 at 3:16

You must log in to answer this question.

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