June 14, 2012

Visualforce Javascript Remoting,Namespace handling improvements in Summer’12 release

image

Javascript remoting is great feature, and I used it a lot in many of force.com projects since feature inception.  Remoting  has gone thru a couple of enhancements in past few force.com releases for ex. previously it was a must for Remoting Apex class to be global, now we this is optional and one can use normal public Apex classes with Remoting as well.

Other major issue with Remoting was regarding namespace prefix handling in prefixed and non-prefixed orgs, a couple of scenarios for that could be:

  1. Unmanaged Packages: Development started in an un-prefixed DE org, and later the package is deployed to other orgs having prefix. Typically this is case with reusable components with in a company, for ex. we developed a pretty cool remoting based charting component to be reused across different consulting projects. But different org prefixes and ISV packages troubled a lot.
  2. Managed Package: ISV team started development started again in a vanilla DE org with no prefixes for namespace, later the same package was pushed to various orgs like QA, UAT and packaging, each having either no prefixes or one.

Solving the namespace prefix problem

We have two solutions available

1. Pre Summer’12 release

@WesNolte came with a nice solution to the problem, described here: http://th3silverlining.com/2012/02/26/salesforce-javascript-remoting-and-managed-packages/

2. Post Summer’12 release

Using remoting is made way easier now, namespaces are handled in similar way like pages, labels etc i.e. a Global variable named “$RemoteAction” is introduced for namespace resolution. It works in pretty much the same way as of $Page or $Label variable, by handing package isolation automatically. So we don’t need to use above pre summer’12 work around any more.

A good example on how to use $Remoteaction is available in official visualforce guide here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

On a side note, I previously posted one use case for Javascript remoting using jQuery templates, this post is now updated to use the latest summer’12 $RemoteAction, have a look here : http://www.tgerm.com/2011/03/javascript-remoting-jquery-templates.html