본문 바로가기

개발자정보

Salesforce(세일즈포스) Action Function

반응형

Action Function is an apex component in visual force page. Apex: Form tag enclosed in the action function tag, which can be able to call the controller method. Action function invoked from the JavaScript as XHR (XML Http Request).   

 <apex:form >
 <apex:actionFunction action="{!register}" name="registerStudent"
 rerender="showstate">
 <apex:param name="studentData" value="" />
 </apex:actionFunction>
 </apex:form>

 

apex:actionFunction

A component that provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component. Because binding between the caller and <apex:actionFunction> is done based on parameter order, ensure that the order of <apex:param> is matched by the caller's argument list.

Use this component to get user input for a controller method that does not correspond to a field on an sObject. Only <apex:inputfield> and apex:outfield can be used with sObject fields.

Unlike <apex:actionSupport>, which only provides support for invoking controller action methods from other Visualforce components, <apex:actionFunction> defines a new JavaScript function which can then be called from within a block of JavaScript code.

Note: Beginning with API version 23 you can't place <apex:actionFunction> inside an iteration component — <apex:pageBlockTable>, <apex:repeat>, and so on. Put the <apex:actionFunction> after the iteration component, and inside the iteration put a normal JavaScript function that calls it.

Example

 
 
 
<!-- Page: -->
<apex:page controller="exampleCon">
    <apex:form>
        <!-- Define the JavaScript function sayHello-->
        <apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out" status="myStatus"/>
    </apex:form>

    <apex:outputPanel id="out">
    <apex:outputText value="Hello "/>
    <apex:actionStatus startText="requesting..." id="myStatus">
        <apex:facet name="stop">{!username}</apex:facet>
    </apex:actionStatus>
    </apex:outputPanel>
            
    <!-- Call the sayHello JavaScript function using a script element-->
    <script>window.setTimeout(sayHello,2000)</script>
            
    <p><apex:outputText value="Clicked? {!state}" id="showstate" /></p> 
            
    <!-- Add the onclick event listener to a panel. When clicked, the panel triggers
    the methodOneInJavascript actionFunction with a param -->
    <apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn"> 
        Click Me 
    </apex:outputPanel>
    <apex:form>

    <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
    </apex:actionFunction>
    </apex:form>
</apex:page>

/*** Controller ***/
public class exampleCon {
    String uname;

    public String getUsername() {
        return uname;
    }
            
    public PageReference sayHello() {
        uname = UserInfo.getName();
        return null;
    }
            
    public void setState(String n) {
        state = n;
    }
            
    public String getState() {
        return state;
    }
            
    public PageReference methodOne() {
        return null;
    }
            
    private String state = 'no';
}

Attributes

Attribute NameAttribute TypeDescriptionRequired?API VersionAccess

 

Attribute Name Attribute Type Description
action ApexPages.Action The action method invoked when the actionFunction is called by a DOM event elsewhere in the page markup. Use merge-field syntax to reference the method. For example, action="{!save}" references the save method in the controller. If an action is not specified, the page simply refreshes.
focus String The ID of the component that is in focus after the AJAX request completes.
id String An identifier that allows the actionFunction component to be referenced by other components in the page.
immediate Boolean A Boolean value that specifies whether the action associated with this component should happen immediately, without processing any validation rules associated with the fields on the page. If set to true, the action happens immediately and validation rules are skipped. If not specified, this value defaults to false.
name String The name of the JavaScript function that, when invoked elsewhere in the page markup, causes the method specified by the action attribute to execute. When the action method completes, the components specified by the reRender attribute are refreshed.
namespace String The namespace to use for the generated JavaScript function. The namespace attribute must be a simple string, beginning with a letter, and consisting of only letters, numbers, or the underscore ("_") character. For example, "MyOrg" and "Your_App_Name_v2" are supported as namespaces. If not set, no namespace is added to the JavaScript functions generated by <apex:actionFunction>, preserving existing behavior.
onbeforedomupdate String The JavaScript invoked when the onbeforedomupdate event occurs--that is, when the AJAX request has been processed, but before the browser's DOM is updated.
oncomplete String The JavaScript invoked when the result of an AJAX update request completes on the client.
rendered Boolean A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
reRender Object The ID of one or more components that are redrawn when the result of the action method returns to the client. This value can be a single ID, a comma-separated list of IDs, or a merge field expression for a list or collection of IDs.
status String The ID of an associated component that displays the status of an AJAX update request. See the actionStatus component.
timeout Integer The amount of time (in milliseconds) before an AJAX update request should time out.
반응형