html code

Apex’s RemoteAction Annotation | Salesforce Apex Developer Guide

Annotations:

An Apex annotation, similar to a Java annotation, alters the usage of a method or class. Annotations commence with a @ symbol, succeeded by the relevant keyword. To include an annotation in a method, indicate the annotation just preceding the method or class declaration. For instance:

Public class Myclass {
    @Future
    Public method A () {
        //Apex code logic
    }
}

This blog will cover the @RemoteAction Annotation.

Annotation: @RemoteAction

The Remote Action annotation enables the invocation of Apex methods in Visualforce through JavaScript, commonly known as JavaScript remoting. Salesforce’s remote action function permits users to access any method from any class using JavaScript methods, with the result returned as a JavaScript object for additional manipulation.

Syntax: Place @RemoteAction before defining the method.

@RemoteAction 
Public Method A () {
    //Apex Code Logic
} 

Note: The method should also be declared as Global or Public and Static.

Example of Remote Action 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;
    }
}

Visualforce 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>