Defining Action Methods

Action methods perform logic or navigation when a page event occurs, such as when a user clicks a button, or hovers over an area of the page. Action methods can be called from page markup by using {! } notation in the action parameter of one of the following tags:

For example, in the sample page described in Using Input Components in a Page, a command button is bound to the save method in the Account standard controller. We can adapt that previous example so that it now uses the MyController custom controller:

<apex:page controller="MyController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock title="Hello {!$User.FirstName}!">
            You are viewing the {!account.name} account. <p/>
            Change Account Name: <p/> 
            <apex:inputField value="{!account.name}"/> <p/>
            <apex:commandButton action="{!save}" value="Save New Account Name"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Note

Note

Remember, for this page to display account data, the ID of a valid account record must be specified as a query parameter in the URL for the page. For example:

https://Salesforce_instance/apex/myPage?id=001x000xxx3Jsxb
Displaying Field Values with Visualforce has more information about retrieving the ID of a record.

After saving the page above, the Visualforce editor offers a “quick fix” option to add the save method to the MyController class. If you click the quick fix link, MyController now looks like this:

public class MyController {

    public PageReference save() {
        return null;
    }

    public String getName() {
        return 'MyController';
    }

    public Account getAccount() {
        return [select id, name from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')]; 
    } 
}

The save method that is generated by the quick fix takes the standard signature for an action method: it is public, returns a PageReference, and contains no arguments.

Ultimately, the save method definition must update the database with new account values, but first we must define a member variable to save the account information that is retrieved from the database. Without a member variable for the account, the record retrieved from the database does not persist after its values are used to render the page, and the user's updates to the record cannot be saved. To introduce this member variable, two parts of the controller code need to change:
  • The member variable must be added to the class
  • The member variable must be set when getAccount performs the initial query
public class MyController {

    Account account;

    public PageReference save() {
        return null;
    }

    public String getName() {
        return 'MyController';
    }

    public Account getAccount() {
        if(account == null) 
            account = [select id, name, site from Account 
                       where id = :ApexPages.currentPage().getParameters().get('id')];
        return account; 
    } 
}

Now that the member variable is in place, all that the save method needs to do is update the database:

public class MyController {

    Account account;

    public PageReference save() {
        update account;
        return null;
    }

    public String getName() {
        return 'MyController';
    }

    public Account getAccount() {
        if(account == null) 
            account = [select id, name, site from Account 
                       where id = :ApexPages.currentPage().getParameters().get('id')];
        return account; 
    } 
}

A more robust solution for save might catch various exceptions, look for duplicates, and so on. Since this is meant to be a simple example, those details have been left out.

To test this page, change the value in the Change Account Name field and click Save New Account Name. As with the standard Account controller example, the page simply refreshes with the new account name. In the next example, we will extend the save action so that instead of refreshing the current page, it navigates the user to a different confirmation page.

Note

Note

For the page to render properly, you must specify a valid account ID in the URL. For example, if 001D000000HRgU6 is the account ID, use the following URL:

https://Salesforce_instance/apex/MyFirstPage?id=001D000000HRgU6
Previous
Next