RemoteAction Annotation

RemoteAction Annotation In Apex | Salesforce Apex Developer Guide

Annotations:

An Apex annotation, like a Java annotation, modifies how a method or class is used. Annotations begin with a @ symbol and are followed by the appropriate keyword. To add an annotation to a method, specify annotation just before the method or class declaration. For example:

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

In this blog, we are going to discuss @RemoteAction Annotation.

Advertisement

dont miss out iconDon't forget to check out: Crash Course on Apex Triggers Salesforce | Complete Guide with Real Time Scenarios

RemoteAction Annotation:

The Remote Action annotation allows Apex methods in Visualforce to be called via JavaScript. This is commonly referred to as JavaScript remoting. The remote action function in Salesforce allows the user to access any method from any class via JavaScript methods, and the result is returned as a JavaScript object for further manipulation.

Syntax- Add @RemoteAction before defining the method

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

Note - The method should also be 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;
    }
}

dont miss out iconCheck out another amazing blog by Parvez here: Integration of Salesforce with Google Calendar Using REST API

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>

Responses

Popular Salesforce Blogs