- Salesforce Blogs -

Apex:Actionfunction In The Salesforce VisualForce Page

JavaScript is the dynamic scripting language that performs actions on the client side. With the help of JS, it is possible reducing traffic on the web server. There are two popular Apex functions the Salesforce - <apex:actionFunction> and <apex:actionSupport> that are used in a Visualforce page to execute different JavaScript tasks. Our main focus is to discuss <apex:actionFunction> in this blog that provides support to invoke controller action methods directly from the JS code using an AJAX request.

An <apex:actionFunction> must be the subset or child of <apex: form> component as binding between the caller and the apex function is done based on order of parameters and makes sure that order of parameters matches completely with by the arguments list of the caller. An <apex:actionFunction> defines the new JavaScript function that can be called within a block of JS code. At the same time, one more popular method <apex:actionSupport> that only provides support for invoking controller action methods.

Based on the API version 23, it is not possible using <apex:actionFunction> within an iteration component. The best idea is to put this Apex function after the iteration component and put a simple JavaScript code within an iteration component. salesforce Curriculum

What are the <apex:actionFunction> attributes?

Attribute name - <action>

This attribute is invoked when the actionFunction is called by the DOM event elsewhere in the page markup. You may use merge-field syntax for method referencing. For example, action = “{save}” references the save method in the controller. When an action is not specified, the page refreshes automatically.

Attribute name - <focus>

It represents the ID of the omponent that is in focus when AJAX request completes.

Attribute name - <ID>

It is an identifier that allows the actionFunction component to be referenced by other components in the page.

Attribute name - <immediate>

It is a Boolean value that specifies whether the action associated with the component should happen immediately or not. It does not any validation rules associated with fields on the page. If it is set to true then action happens immediately and validation rules are skipped automatically. If there is no value defined for the attribute then it is set to false by default.

Attribute name - <name>

It includes the name of JavaScript function when the function is executed somewhere else in the page markup causes the method specified by action attribute to execute. As soon as the action method completes, the components specified by the reRender attribute are refreshed.

Attribute name - <namespace>

The namespace is used for the generated JavaScript function. The attribute of namespace should be a simple string, beginning with a letter and consists of only letters, numbers, or the underscore character. For example, your app_name_version is supported as namespaces. If no namespace is added to the JavaScript functions that are generated by the <apex:actionFunction>, then it preserves the existing behavior.

Attribute name - <OnComplete>

The JavaScript function invoked when the result of an Apex update request completes on the client.

Attribute name - <rendered>

It includes the Boolean value that specifies whether the component is rendered or not on the web page. If not specified, the value is set to true by default.

Attribute name - <reRender>

It shows the ID of one or more components that are redrawn when the result of the action method returns to the client. The final value can be an individual ID, a comma-separated list of multiple IDs, a merge field expression for the list of the collection of IDs.

Attribute name - <status>

It is the ID of an associated component that displays the status of an AJAX update request.

Attribute name - <timeout>

It shows the amount of time in milliseconds before an AJAX request should time out.

Why action tags are needed in the Salesforce?

It is possible creating Visualforce pages by invoking controller methods using AJAX request. There is the case you want to use AJAX in the Visualforce page so that a particular part of the page can be refreshed and performs specified actions when the user clicks on a button. We can use multiple action tags in this case that are supported by AJAX request.

<apex:actionFunction>

This action tag must be called from the client-side event or from a custom JavaScript function. It generally behaves like any other JavaScript function that has the capability to communicate with server methods using the “action” attribute. Keep in mind that action attribute is optional here that should be used when required. This action tag can also refresh the VF function on the client-side event using the reRender attribute.

<apex:actionSupport>

This action tag adds AJAX support to another Visualforce component and then call the controller method. For example, we wanted to implement the search functionality where we need instant results for each keyword that matches the content. Here, we need to add AJAX support to the InputText that calls a function and returns the needed result.

<apex:actionPoller>

This action tag acts as the timer on the Visualforce page. It is used to send the AJAX request to the server depending on the time interval. In case, the time span is not specified then it is taken 60 seconds by default. salesforce quiz For example, we want to update the account list in every 5 seconds. It will show every new account on the Visualforce page as soon as added. It is clear from the discussion that this action tag is used for the component that we want to refresh in every few seconds. If you don’t want to refresh the component frequently then you should use <apex:actionRegion> in this case.

How to pass parameters between Apex and Visualforce?

When the user clicks a link, we want to pass the ID to the Apex method. The APEX method then returns to us a URL string. We open a new window here that loads that needed URL. We can call parameters and variables from Visualforce pages but passing parameters or variables to and forth between Apex methods and Visualforce is not intuitive. However, it is impossible passing parameters from a Visualforce page to the Apex method directly.

  • Not Supported: It is possible calling Apex method from a Visualforce page directly if parameters are required.
  • Supported: It is possible calling Apex method from a Visualforce page if there are no parameters.

So, how to pass the ID parameter to the Apex method from the Visualforce page. Well, Apex class is the solution here that help to initialize variables outside the method since parameters cannot be passed from Visualforce page directly. We need to declare actionFunction here that works similar to the Apex method. When you call this action tag, you are free to add multiple parameters between Apex and Visualforce method here. free salesforce demo Conclusion

With this discussion, you must be sure of what is Apex:actionFunction method and how it helps in multiple ways to invoke controller methods using different action control attributes. There are more similar methods too that is also discussed in brief because the main objective of this blog is to discuss the concept of Apex:actionFunction in detail. To know more on tough Apex methods and controllers, join Salesforce certification program at JanBask Training and learn using different control tags practically with the right experts’ assistance.

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Comments

Salesforce Upcoming Batches

Mon - Fri

6 Weeks

Mon - Fri

6 Weeks

Newsletter