Remote Action (JavaScript Remoting) — Overview
Remote Action (also called JavaScript Remoting) allows Visualforce pages to call Apex methods directly from client-side JavaScript. It provides a lightweight, asynchronous way to invoke server-side logic without a full page postback, improving UX and reducing network overhead. Remote Action is implemented using Apex methods annotated with @RemoteAction and JavaScript calls that serialize parameters and handle responses via callbacks.
Why use Remote Action?
Remote Action is useful when you need to:
- Perform asynchronous server calls from a Visualforce page without re-rendering the whole page.
- Improve perceived performance by fetching or updating small pieces of data dynamically.
- Reduce view state and avoid heavy form submissions for simple operations.
- Integrate complex client-side UI (e.g., single-page interactions) with Apex business logic.
Key characteristics
- Calls are asynchronous and use a callback function signature:
function(result, event). - Apex methods must be
staticand annotated with@RemoteAction. - Data is serialized as JSON between client and server.
- Remoting calls bypass full Visualforce form submission and view state.
Basic example
Server-side Apex controller:
public with sharing class AccountController {
@RemoteAction
public static Account getAccountById(Id accId) {
return [SELECT Id, Name, Phone FROM Account WHERE Id = :accId LIMIT 1];
}
}
Client-side JavaScript on a Visualforce page:
AccountController.getAccountById('{!Account.Id}', function(result, event) {
if (event.status) {
// success - result contains the Account object
console.log('Account name: ' + result.Name);
} else if (event.type === 'exception') {
console.error('Exception: ' + event.message);
} else {
console.error('Error: ' + event.message);
}
});
Callback event object
The callback receives two parameters: the result (deserialized JSON) and an event object. The event contains useful properties:
status— boolean, true on success.message— error message whenstatusis false.type— ‘exception’, ‘exceptionType’, ‘apex’, etc.where— stack trace info for server exceptions.
Limitations and best practices
- Governor limits still apply — each remoting call counts as synchronous Apex execution.
- Keep payloads small to reduce latency; avoid returning huge collections or blobs.
- Use pagination or selective SOQL to limit data returned.
- Validate and enforce sharing/security in Apex (
with sharingif required). - Handle error cases robustly in the client callback.
- Because methods are static, avoid using instance state; pass all required data as parameters.
When to prefer Remote Action
Choose Remote Action when you have a Visualforce-based UI and need fast, JS-driven interactions that call Apex. For Lightning Components or modern frameworks, consider Lightning Data Service, LWC wire/adapters, or Platform REST APIs instead.
Security considerations
Remote Action uses the running user’s permissions and sharing rules as enforced in Apex code. Always perform server-side validation and enforce field- and object-level security where needed. Avoid exposing sensitive data through remoting calls unless properly secured.
Summary
Remote Action (JavaScript Remoting) is a powerful tool for integrating client-side JavaScript with Apex in Visualforce pages. It enables asynchronous, JSON-based calls to static Apex methods annotated with @RemoteAction, improving responsiveness and reducing full page postbacks when used properly and securely.








Leave a Reply