Visualforce Custom Controllers

,

You have accomplished a lot with Visualforce–shaping the look and feel of your Visualforce pages, using standard controllers to implement rich functionality, and extending those controllers when something a little extra is required. And your guiding philosophy for the Force Platform should be to always use as much default functionality as you can.

But Visualforce can take you even further. You can create your own custom controllers to back up your Visualforce pages. A controller extension extends or overrides standard controller functionality with a little bit of Apex code in a class. A custom controller completely replaces a standard controller with an Apex class.

In this final section on Visualforce, you create a custom controller to implement a wizard-like interface on the Force Platform. In this example, several different Visualforce pages access the same custom controller. Users navigate between pages, but the custom controller maintains the data over multiple Visualforce pages.

Important: Saving state

Normally, when a controller redirects the user to another page, the state for the controller is flushed. The only way to avoid this is to use the same controller for multiple pages, with that controller handling the forwarding to different pages.


The wizard you will implement gives users a fast path to creating an interview schedule for a candidate for a position. The user selects from a list of positions, sees the candidates for that position, and then selects an interview schedule for that candidate, using three Visualforce pages and a custom controller.

Creating a Custom Controller

Your first step is to create the skeleton of the custom controller, which is an Apex class.

1.
Go to Setup Develop Apex Classes and click New.

2.
Enter the following code for the class, and do a Quick Save to valid your syntax and save the code:

public class customController {
}

This basic code simply creates the Apex class identified as the controller for the Visualforce pages.

3.
To support the first page, which you create in the next part, add the following code, which is also available from the Code Share project for this book:

public class customController {
public List<selectOption> openPositionList {
  get {
    List<selectOption> returnOpenPositions =
     new List<selectOption>();
    String positionInfo;
    for (Position__c p : [select name, job_description__c,
      department__c, position_type__c, status__c
      from Position__c where status__c = 'Open']) {
        positionInfo = p.name + ' - ' + p.department__c
         + ' - ' + p.job_description__c;
        returnOpenPositions.add(new selectOption(p.ID,
          positionInfo));
      }
    return returnOpenPositions;
    }
    set;
  }
public Position__c selectedPosition {
  get {
    if(selectedPosition == null) {
      selectedPosition = new Position__c();
      }
    return selectedPosition;
    }
    set;
}
public PageReference step2() {
  return null;
}
public PageReference step3() {
  return null;
}
}

The controller returns a list of selectOptions, with the value being the ID of the selected Position records and the label concatenated from several of the fields.. The SOQL query to populate this list has a where clause, limiting the positions displayed to those with a Open status, as the process flow at Universal Containers will only allow interviews to be set up for any open positions.

The final property defined in the current code, selectedPostion receives the position selected by the user in the first page of the wizard, which will be used to limit the selection in the second page of the wizard. The property uses an if statement to initialize itself, if this has not already been done, which is determined by checking for the property having a null value.

The class also contains two methods, step2 and step3, used to navigate to the second and third pages of the wizard. These methods return a PageReference. When a PageReference is returned, the page referenced by the URL is returned to the browser from this function. The PageReference data type automatically uses the correct URL for the current Force Platform instance as the prefix for the URL, so returning null will return the user to the main page for the instance.

Right now, these methods cannot define the PageReferences to which the method navigates since these page references refer to Visualforce pages that are not yet created. You will modify these methods once you create the second and third pages of the wizard.

This small amount of code is all that you need to power the first page of the wizard. You are still be able to use all the default functionality in Visualforce page components, as you will see in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.52.33