Wednesday, March 14, 2012

Using the Standard Controller save method with an Extension Controller

When using an Extension Controller it can be useful to wrap the save method of the Standard Controller with a custom save method. This allows for things like:

  • custom validation
  • custom field assignment
  • altering the resuling PageReference redirection
public with sharing class CustomExtensionController {
    
    Opportunity opp;
    ApexPages.StandardController sController;
    
    public CustomExtensionController (ApexPages.StandardController controller) {
        sController = controller;
        opp = (Opportunity)controller.getRecord();
    }
    
    public PageReference saveCustom() {
        
        // Custom Save code here...
        
        PageReference pr = sController.save();
        return pr;
        
        // Alternatively, could return a PR to the view page.
        //pageReference pv = sController.view();

    }
}