[SalesForce] Modify visualforce input before saving and before reaccessing

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!!

Best Answer

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 = '';
}
Related Topic