JavaScript Remoting

Normally, when a form is submitted, the whole page gets reloaded. To avoid the page loading for every click and validation, we can use scripting languages. As a result, the loading time will decrease and performance will increase.

Normally, when a form is submitted, the whole page gets reloaded. To avoid the page loading for every click and validation, we can use scripting languages. As a result, the loading time will decrease and performance will increase.

Architectural Overview: 

JavaScript Remoting allows you to communicate with the controller without posting the form asynchrously. It asynchronous AJAX requests  asynchronous AJAX requests  asynchronous AJAX requests will provide maximum efficiency and performance of the Visualforce page, and it is the most efficient way of calling the controller. It provides more responsive experience in both Mobile pages and web pages.

Once the user clicks the button, the controller method can be called through JavaScript. In JavaScript, we can include any validation if needed. After the controller method gets executed, we will have callback function which returns the response or status of the request.

javascript remoting

Key Features are: 

  1. Remote Action is a static method
  2. Bandwidth Usage is less
  3. View state is not maintained

Let’s see a sample implementation of the JavaScript Remoting.

Step: 1 

Create an Apex controller class and it should have sharing keyword and an @RemoteAction attribute to call that controller function in JavaScript.

In this example, getContact method accepts contact name as a parameter and returns the list of contacts that match the name

javascript remoting

Step: 2  

Create a Visualforce page to a call controller method from JavaScript

Syntax to call RemoteAction method

[namespace.]controller.method( 

    

[parameters…,]

callbackFunction,

[configuration]

);

NamespaceNamespace of our Apex controller class. 
ControllerIt is the name of our Apex controller class
MethodIt is the method name.
ParametersList of parameters for our function
CallbackFunctionIt will have status and result for the method call.
ConfigurationIt has config information for remote call. The default value is {escape: true}.
javascript remoting

Step: 3 

Finally, run the Visualforce page.

javascript remoting salesforce

Pros: 

  1. Bandwidth usage is less
  2. View state is not maintained
  3. Has the callback option.

Cons: 

  1. Need  basic knowledge of JavaScript and AJAX
  2. Asynchronous nature may cause conflicts or unexpected behavior

Summary: 

JavaScript Remoting is widely used for handling multiple sections on the page and processing large amounts of data from the controller. For example, if you are using a nested <apex:repeat> or <apex:pageblocktable>,  the Viewstate of the page increases extensively. Real time Scenarios are:

  1. Auto-complete search functionality.
  2. Any situation in which you don’t need any partial page rendering and just want to perform an action.
  3. Any situation where you need to repetitively call controller without the need to rerender.

Web Reference Links: 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_summary.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_when_to_use.htm
https://www.w3schools.com/js/

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Session Based Permission Sets

A Permission Set is a collection of settings and permissions that give access to users to various tools and functions. The settings and permissions in Permission Sets are additionally found in profiles, but permission sets broaden users’ functional access without altering their profiles.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.