What is JavaScript Remoting in Salesforce?

JavaScript remoting (Js Remoting) is a way to call methods in Apex Classes directly from JavaScript. It allows you to perform tasks on the page without having to reload the entire page.

There are several benifits for using Js remoting for apex controllers:-

  1. Java Script remoting does not store view state, and thus helps to minimize the view state limits.
  2. We can invoke any controller method directly from the javascript defined in VF page.
  3. It works on client side, results are very fast.

For Example: We want to create a Contact record using Js Remoting and navigate to the record detail page upon creation.

Visualforce Code:

<apex:page controller="ContactRemoterClass">

 function createConJsRemoting() {
 
 var conLastName = document.getElementById('ContactName').value;
 
 Visualforce.remoting.Manager.invokeAction(
 '{!$RemoteAction.ContactRemoterClass.CreateContact}',
 conLastName , 
 function(result, event){
 if (event.status) {
 alert('Contact Created.');
 window.location.href= '/'+ result.Id;
 } 
 }, 
 {escape: true}
 );
 }
 
 
 <b>Contact Last Name: </b><input id="ContactName" type="text"/>
 <button onclick="createConJsRemoting()">Create Contact (JS Remoting)</button>
 
</apex:page>

In the above VF code, we are calling javascript method createConJsRemoting() on button click. This javascript method invokes server side controller method CreateContact() using Java script remoting.

Salesforce provides Visualforce.remoting.Manager.invokeAction method to call call APEX methods directly from javascript. This method accepts few parameters.

  1. The server side method {!$RemoteAction.ContactRemoterClass.CreateContact}
  2. The parmeter(s) to be passed to the server side method.
  3. The last parameter is a function which handles the logic for post call to server method (call back handler)

The result variable represents the Contact object returned from the controller CreateContact() method. The event.status variable is true only if the call was successful.

Controller Code:

public class ContactRemoterClass{

 @RemoteAction
 public static Contact CreateContact(String conLastName ) {
 Contact c = new Contact(LastName = conLastName);
 insert c;
 
 return c;
 }

}

In the above apex code, we have used annotation @remoteAction which allows this method to be called by Java script remoting manager.

Summary:

We can always JS Remoting where we want to eliminate view state, client side functioning and fast response.

 
Author:

Ajomon Joseph AJOMON JOSEPH
Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog https://apexcoder.com
email Email me ajomon@apexcoder.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s