Similar to Java annotations, an Apex annotation alters the usage of a method or class. Annotations commence with the ‘@’ symbol followed by the relevant keyword. To include an annotation within a method, indicate it immediately prior to the declaration of the method or class. For instance:
Public class Myclass { @Future Public method A () { //Apex code logic } }
In this article, we will be exploring the @RemoteAction Annotation.
Annotation for RemoteAction:
The RemoteAction Annotation enables JavaScript to call Apex methods within Visualforce. This is often known as JavaScript remoting. Within Salesforce, the remote action function permits users to utilize JavaScript methods to invoke methods from any class. The outcome is then provided as a JavaScript object, allowing for subsequent manipulation.
Syntax: Precede the method definition with @RemoteAction.
@RemoteAction Public Method A () { //Apex Code Logic }
Note: The method must also have the Global or Public and Static access modifiers.
Example of RemoteAction Annotation:
Apex Class ->
Public Class MyClass { List<Account> AccList {get;set;} @RemoteAction Public static List<Account> getList() { AccList = [Select Id, Name, phone from Account]; return AccList; } }
VF Page ->
<apex:page controller = “MyClass”> <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" /> <script type="text/javascript"> demotask(); function demotask() { Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.MyClass.getList}', function (result, event) { //Code Logic }, {escape: true}); } <div> <!-- Here You can Print your Acclist List content which is now stored in Result parameter of demotask JS function ---> </div> </apex:page>