---
schema_version: "1"
id: 91fda0736c6a8d80615c421f36bba1d837fe8d6254ab700b05e31f911d7fe689
title: Defining Action Methods
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/quick-start/advanced/pages-quick-start-controller-setter-methods.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-quick-start-controller-setter-methods.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 280c9cda80d3d048bc69715e70fea8b5bfcb58497bc506a336ac887ad8f6fe2f
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# 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:

-   [`<apex:commandButton>`](https://developer.salesforce.com/docs/platform/components/pages-compref-commandbutton.md) creates a button that calls an action
-   [`<apex:commandLink>`](https://developer.salesforce.com/docs/platform/components/pages-compref-commandlink.md) creates a link that calls an action
-   [`<apex:actionPoller>`](https://developer.salesforce.com/docs/platform/components/pages-compref-actionpoller.md) periodically calls an action
-   [`<apex:actionSupport>`](https://developer.salesforce.com/docs/platform/components/pages-compref-actionsupport.md) makes an event (such as “onclick”, “onmouseover”, and so on) on another, named component, call an action
-   [`<apex:actionFunction>`](https://developer.salesforce.com/docs/platform/components/pages-compref-actionfunction.md) defines a new JavaScript function that calls an action
-   [`<apex:page>`](https://developer.salesforce.com/docs/platform/components/pages-compref-page.md) calls an action when the page is loaded

For example, in the sample page described in [Using Input Components in a Page](https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/pages-quick-start-input-components.md#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:

<!-- otherprops: apex_page set_clipboard -->
```markup
<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:** 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:

```nolang
https://MyDomain_login_URL/apex/myPage?id=001x000xxx3Jsxb
```

[Displaying Field Values with Visualforce](https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/pages-quick-start-display-field-values.md) 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:

<!-- otherprops: apex_code set_clipboard -->
```controller
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

<!-- otherprops: apex_code set_clipboard -->
```controller
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:

<!-- otherprops: apex_code set_clipboard -->
```controller
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:** 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:

```nolang
https://MyDomain_login_URL/apex/MyFirstPage?id=001D000000HRgU6
```

