Jun 19, 2011

Summer 11 Features - Part 2 (JavaScript Remoting)

One of the major enhancements from visualforce this release(Summer 11) and my favorite is JavaScript remoting.


JavaScript Remoting is the ability to invoke apex class method from javascript embedded in a visualforce page. JavaScript Remoting is now available after summer 11 release.


Here is a quick example : we have a Apex class which defines a get Account method.
global with sharing class MyJSRemoting
{
    public static Account account { get; set; }
     
    @RemoteAction
    global static Account getAccount(String accountName) {
        account = [select id, name, phone, type, numberofemployees from 
             Account where name = :accountName limit 1];
        return account;
    }   
}
This method is having @RemoteAction annotation making it available to be called from a visualforce page. Now in our visualforce page we have a script tag set which is how we embed javascript within visualforce pages.




    
    
    
    
    
        
    
            
                 
            
    
            
                
            
    
        
    


This JavaScript code then invokes the getAccount method in the apex class to take action.


Please note I have used namespace "ankit.MyJSRemoting" as there is a namespace registered on my organization.

JavaScript Remoting is the feature which is primarily be used by developers and will allow them to create richer and more interactive user interfaces that everyone will benefit from.

In particular, lets developers create rich javascript based user interfaces by invoking apex controller methods within JavaScript code. JavaScript is typically used to render small parts of user interface. Result from a Javascript remoting call are much faster then using a typical visualforce reRender model, developers can provide more instant feedback to the user. This is because remoting calls are stateless. Means only data is set back and forth instead a lot of bulky HTML.

Additional Visualforce enhancements :

1) Filtered Lookup via Visualforce.
2) Inline editing for rich text area field.
3) Field Set property accessors.

4 comments:

  1. Hi Ankit,
    I have used this code in my DE,
    But it's not working, the javascript is unable to invoke the Controller method(i.e., @RemoteAction method).
    Class is invoking & Class Constructor is invoking.
    Please let me know this...........!
    regards
    RB

    ReplyDelete
  2. Please send me your code (Apex class and visualforce) at ankit.salesforce@gmail.com

    ReplyDelete
  3. Hello Ankit..will you please explain how to do inline editing using remote action.

    ReplyDelete
  4. I am happy to read this post because this code useful for me.
    java script certification

    ReplyDelete