You need to sign in to do that
Don't have an account?

hi can any one explain when to use ACTION FUNCTION and REMOTE ACTION in detail i much confusion on these both
hi can any one explain when to use ACTION FUNCTION and REMOTE ACTION in detail i much confusion on these both
ACTION FUNCTION:
ActionFunction is used to execute a method in your Apex Class from within your Visualforce Page asynchronously via AJAX requests. What does asynchronous AJAX requests mean ? This means that Visualforce Pages(otherwise HTML pages when rendered at the Client Side via the Browser) can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page. So when we execute an Apex Method via the ActionFunction, the page is not disturbed and the request to the servers(Apex Code compiles and runs on the Salesforce servers while the Visualforce pages which are nothing but HTML pages are rendered by browser at the Client Side) are sent and received in the background. The other way of doing such AJAX requesst to Apex Methods include the Visualforce Remoting. The only difference between the same is that when using the Visualforce Remoting you will have to write some extra lines of JavaScript which is not needed in case of the ActionFunction.
Now, a very simple and a common example of ActionFunction is mimicking the Field Dependency feature on Visualforce Pages. Say for example you had two Picklists – Select the Object and Select the Field. When a user selects an Object, the next Picklist automatically gets populated with the Fields that belong to the object.
Well, I will not be using this one since that might get a bit complicated with couple of Describe calls but let us consider an another one. For example say you had two Picklists like this- Select the Alphabet(from which you can select from choices like –A, B, C and D) and then a Select the Fruit(from which you can select the fruit of your choice). So when a User selects A, all the Fruits starting with A gets populated in the 2nd picklist and when the User selects B, all the Fruits starting with B gets populated and so on.
Controller:
VisualForce Page:
REMOTE ACTION:
@RemoteAction in Visual force page
JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.
JavaScript remoting has three parts:
-
The remote method invocation you add to the Visualforce page, written in JavaScript.
-
The remote method definition in your Apex controller class. This method definition is written in Apex, but there are few differences from normal action methods.
-
The response handler callback function you add to or include in your Visualforce page, written in JavaScript.
To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following form:[namespace.]controller.method(
[parameters...,]
callbackFunction,
[configuration]);
-
namespace is the namespace of the controller class. This is required if your organization has a namespace defined, or if the class comes from an installed package.
-
controller is the name of your Apex controller.
-
method is the name of the Apex method you’re calling.
-
parameters is the comma-separated list of parameters that your method takes.
-
callbackFunction is the name of the JavaScript function that will handle the response from the controller. You can also declare an anonymous function inline. callbackFunction receives the status of the method call and the result as parameters.
-
configuration configures the handling of the remote call and response. Use this to specify whether or not to escape the Apex method’s response. The default value is {escape: true}.
VisualForce Page:Controller:
Thanks,
Rockzz
Use JavaScript remoting in Visualforce to call methods in Apex controllers from JavaScript. Create pages with complex, dynamic behavior that isn’t possible with the standard Visualforce AJAX components.
here small example:
Controller:
Your VF / Javascript:
[And remember, if it helps, mark it as "Best Answer", if not ... reply ]
Thanks,
Rockzz
1.Action Function doesn't return anything but by using remote action you can get return value from controller to visualforce page. so if there is any requrement that you have to process something on the basis of result then use RemoteAction
2. Remoteaction will not add viewstate to visual force page. so while handling large amount of data use RemoteAction
Thanx,
Atul Patil
Salesforce Developer
www.zen4orce.com