Archive for June 2012
Salesforce JavaScript Remoting: Using Apex and JavaScript objects to pass data from client- to server-side and vice versa
I’ve spoken about how to do this at a high-level during Cloudstock London and there are hints at how it can be done but no formal documentation that I’ve found, so here we are 🙂
Quite simply JavaScript Remoting will transform Apex objects and classes (or collections of these types) into JavaScript objects for you. The opposite is true too but there are some rules you need to observe.
Apex Types to JavaScript Equivalents
This is the easier of the type conversions in that you don’t have to really do anything to make it happen. The code below uses a custom class that I’ve defined but you can do the same with any sObject too. Let’s have a look at the code.
The Controller
public with sharing class RemotingObjectsController { /* The remoting method simply instantiates a two custom types, puts them into a list and then returns them. */ @RemoteAction public static List<CustomClass> getClassInstances(){ List<CustomClass> classes = new List<CustomClass>(); CustomClass me = new CustomClass('Wes'); CustomClass you = new CustomClass('Champ'); classes.add(me); classes.add(you); return classes; } /* My custom type */ public class CustomClass{ public String firstName{get;set;} CustomClass(String firstName){ this.firstName = firstName; } } }
The Visualforce
<apex:page controller="RemotingObjectsController"> <script> // Will hold our converted Apex data structures var classInstances; Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.RemotingObjectsController.getClassInstances}', function(result, event) { // Put the results into a var for pedantries sake classInstances = result; console.log(classInstances); // Assign the first element of the array to a local var var me = classInstances[0]; // And now we can use the var in the "normal" JS way var myName = me.firstName; console.log(myName); }); </script> </apex:page>
The Output

Console output from the JS code.
JavaScript Types to Apex Equivalents
This is a little tricker, especially when it comes to sObjects. Note that the approach below works for classes and sObjects too.
The Visualforce Page
<apex:page controller="RemotingObjectsController"> <script> /* Define a JavaScript Object that looks like an Account */ /* If you were using custom objects the name must include the "__c" */ function Account(){ /* Note the field names are case-sensitive! */ this.Id = null; /* set a value here if you need to update or delete */ this.Name = null; this.Active__c = null; /* the field names must match the API names */ } var acc1 = new Account(); acc1.Name = 'Tquila'; acc1.Active__c = 'Yes'; var acc2 = new Account(); acc2.Name = 'Apple'; acc2.Active__c = 'Yes'; var accounts = new Array(acc1, acc2); Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.RemotingObjectsController.insertAccounts}', accounts, function(result, event) { console.log(result); }); </script> </apex:page>
The Controller
There not much to the controller in this case.
public with sharing class RemotingObjectsController { @RemoteAction public static void insertAccounts(List<Account> accounts){ insert accounts; } }
Why is this cool?
Good question. If the Force.com Platform didn’t do this for you then we – the developer – would need to convert ours types explicitly on both the server-side and the client-side, and man-oh-man is that boring, error-prone work. Yet again the guys at salesforce.com have built in a convenience that saves us time and let’s us get on with the work of building cool apps.