Actionfunction in Salesforce :- In our previous Salesforce Developer Training Tutorial we have clearly learned about Refreshing visualforce page partially using reRender attribute.In this visualforce Tutorial we are going to learn about Actionfunction in Salesforce example, how actionfunction is used in salesforce.

What is an Actionfunction component?

Action function component triggers an action if a user  clicks on any  custom link or button used in visualforce page. In visualforce action function component is used to call method in a controller as a JavaScript function. Action function component can be used any where in the visualforce page to call action which include custom JavaScript code.

Actionfunction Example :

<apex:actionfunction name=”timestwofunction” action=”{!timestwo}” reRender=”result”>.

Let us see an example on action function component.

  • Create an visualforce page.

Action function in Salesforce Example

  • Click on create page actionfunction link as shown below.

Action function in Salesforce Example

  • Create an apex class as show below.

Actionfunction in Salesforce Example

Visualforce page:-

Actionfunction in Salesforce Example

To invoke an action by an actionfunction component we have to invoke action in action attribute(ex:- action=”{!timetwo}”) and JavaScript name is mentioned in the name attribute(ex:- name=”timestwofunction”). All these attributes must be written in between <apex:form> and </apex:form> tag.

<apex:param> is used to define arguments on the function. Param component must be used inside actionfunction component.

From this Salesforce Tutorial we creating visualforce page using action function component to multiply a number with two and reRender attribute which partially refreshes visualforce page. A value can be multiplied with value two using a controller method and JavaScript function.

Action function Output.

When we click on Save button the output will be as shown below.

Actionfunction in Salesforce Example

When we click on Run the result will be 2.

Actionfunction in Salesforce Example

When we click on Run again the result value is multiplied with 2 and displayed a 4.

Actionfunction in Salesforce Example