1

Here's the scenario.

(a) In standard controller extension for Custom_Object__c, we define custObj as

public Custom_Object__c custObj{get;set;}

(b) In visualforce we have an input field

<apex:inputField id="commentfield" value="{!custObj.Comments__c}"/>

Now here's the scenario i was wondering if could be implemented or not,

  1. User enters his comment in commentfield.
  2. A method in controller extension modifies the input here before saving. (For eg. add system.now() timestamp after the comment) [[How will this method access the entered value to modify it and save it back to the record??]]
  3. Custom Object's record is saved with the modified input via standard {!Save} method in visualforce.
  4. When user access the same record this field needs to appear blank(How to make it appear blank in VF page or should I clear the field value right after saving?) and when the user enters new comment, that comment should have it's own timestamp and be concatenated with comment in step 1-3.

Thanks for the insights!!

2 Answers 2

1

For the first approach you can try to use the following code as pattern your implementation. First add a timestamp as a string to the Comment__c field. Then save the record and redirect user to this record:

public PageReference save(){
    // First concatenating the old value with a new one
    custObj.Comments__c = dummyObject.Comments__c;
    // Now adding a timestamp to the new comment 
    custObj.Comments__c += ' ' + Datetime.now().format('MM/dd/YYYY-HH:mm:ss');
    // Upserting the record
    upsert custObj;
    // Redirecting user to the record
    PageReference pRef = PageReference('/' + custObj.id);
    return pRef;
}

And for the next appoach i would create a dummy object where you can temporary hold the original data for the future use:

public Custom_Object__c dummyObject;

// Constructor of the controller
public YourController(){
    dummyObject = custObj.Comments__c;
    custObj.Comments__c = '';
}
1
  • Thank you for a detailed guidance mastOr! Will update you once I try.
    – Raish
    Dec 19, 2013 at 4:11
2

There are a couple of approaches that come to mind.

  1. Don't reference the field directly from the VisualForce page - instead, reference a string property that is initially blank. Then, have your save method set the comment field based on that string property (appending the value and date to the current field value) before calling the standardcontroller save, and clear the string property.

  2. Use a Before trigger on the custom object to detect the new field value, and replace it inside the trigger with the old field value plus the new value and date. You'll still need to use a separate string property if you want the comment field to appear blank on the page instead of reflecting the current field value.

The latter approach generalizes the solution to work with Apex as well as the VisualForce page - not sure if that's something that interests you.

Hope this helps

1
  • Thank you kibitzer. Will give your approach 1 a try. Appreciate your help!
    – Raish
    Dec 19, 2013 at 4:10

You must log in to answer this question.

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