Visualforce Remote Objects

One of the exciting feature of Spring14 release is introduction of “Visualforce Remote Objects”. You can say its actually replacement of JavaScript Remoting.

Why do we need “Visualforce Remote Objects” when we already have “JavaScript Remoting” ?

Well, here are few advantages of “Visualforce Remote Objects” :

  1. No need to write Controllers, Everything can be done in Visualforce only.
  2. As @RemoteAction annotated methods needs to be static so you had to take special precaution as it didn’t supported Viewstate. This hurdle is completely removed now.
  3. No need to write Test Class now, as no Controller is involved.

How to start with this ?

At time of writing this article, This feature is under Pilot release. So, you have to contact Salesforce support to enable it in your Organization.

Visualforce code Sample :

<!-- This Demo will assume Querying Account Object -->
<apex:remoteObjects>
<apex:remoteObjectModel name="Account" jsShorthand="getActs" fields="Name,Id">
 <apex:remoteObjectField name="ClientType__c" jsShorthand="cType">
</apex:remoteObjectModel>
</apex:remoteObjects>

you can see in above code, few new Visualforce tags are introduced like “remoteObjectModel” and “remoteObjectField“.

jsShorthand attribute defines shortcut that can be used inside your JavaScript code. Now, we don’t have to write annoying object or field name ending with “__c” or namespace name. This will keep our code tidy.

Javascript code Sample :

//Create new Remote Object Reference
var src = new SObjectModel.getActs();

//Use Remote Object to query 5 records
src.retrieve({
			limit : 10,
			where : {
				cType :{
					eq : 'Banking'
					}
			}
		} ,
		function(err,records){
			if(err == null)
			{
				//Process returned "records" to display in Visualforce code.
			}
} );

In above code, we are calling retrieve() method of Javascript object SObjectModel. Once you get records, you can process it in your javascript. Other than retrieve() , you can perform all CRUD operations on object.

You can see below articles also on same topic.

Posted

in

by


Related Posts

Comments

16 responses to “Visualforce Remote Objects”

  1. Harshit Pandey Avatar

    This is good, will try this out. What about developer account, not sure if we get this enable for free

    1. JitendraZaa Avatar
      JitendraZaa

      yeah Its Free @facebook-704467938:disqus , we need to create case with Salesforce on Spring14 enabled org.

  2. Harshit Pandey Avatar

    @JitendraZaa:disqus – Wow, strange, I am having trouble in opening case with my free developer account. Getting redirect to login page and making through login. I am back to same page I was. It looping to and fro.

  3. Guest Avatar
    Guest

    How would you create a contact using this dml operation ; here is what I tired creating a contact though, it din’t worked for me as of now, playing around with it. Retrieve is simple but I still need more documentation on applying filter to data retrievals

    //Method to create contact

    var createContact = function() {

    var new_contact = new SObjectModel.Contact({ FirstName: “Arjit”, LastName: “Singh”});

    new_contact.create(function(error,result,event) {
    if(error==null){
    alert(“Contact created”);
    }

    else {
    alert(error);
    }
    });

    }

  4. Guest Avatar
    Guest

    JitendraZaa How would you create a contact using this dml operation ; here is what I tired creating a contact though, it din’t worked for me as of now, playing around with it. Retrieve is simple but I still need more documentation on applying filter to data retrievals

    //Method to create contact

    var createContact = function() {

    var new_contact = new SObjectModel.Contact({ FirstName: “Arjit”, LastName: “Singh”});

    new_contact.create(function(error,result,event) {

    if(error==null){

    alert(“Contact created”);

    }

    else {

    alert(error);

    }

    });

    }

  5. Harshit Pandey Avatar

    @JitendraZaa:disqus

    How would you create a contact using this dml operation ; here is what I tired creating a contact though, it din’t worked for me as of now, playing around with it. Retrieve is simple but I still need more documentation on applying filter to data retrievals

    //Method to create contact

    var createContact = function() {

    var new_contact = new SObjectModel.Contact({ FirstName: “Arjit”, LastName: “Singh”});

    new_contact.create(function(error,result,event) {

    if(error==null){

    alert(“Contact created”);

    }

    else {

    alert(error);

    }

    });

    }

    1. JitendraZaa Avatar
      JitendraZaa

      I am not able to specify Account. It is giving me error like Contact.Account is invalid field.. Any luck from you ?

      1. Prabhan Avatar
        Prabhan

        hi

        Did u try “contact.accountid” ?

  6. Gangparia Avatar
    Gangparia

    Working fine for me

    var new_contact = new SObjectModel.Contact({ FirstName: “James”, LastName: “Gang”})

    new_contact.create(function(error,result,event) {

    console.log(error);

    console.log(result);

    console.log(event);

    });

  7. k mahesh Avatar
    k mahesh

    Hi,

    using javascript Remoting ,How to display Dynamically comming
    multipicklist vales as checkboxes in an visualforce page?

  8. Sakthivakeswaran Avatar
    Sakthivakeswaran

    @JitendraZaa:disqus

    I am working with visual force Remote action to retrieve some records on a button click from salesforce. It works like charm when i call the function contains this remote action.
    But After leaving the page idle for some time , when i try calling the same remote action it throws error like image below. I could not able to find what would be the error. When i drill down to the response of remote action, it returs a html element instead of requested data.
    I’ve came over some links and it doesn’t solves my problem. could you kindly provide some insight with this issue?

    1. Jitendra Zaa Avatar

      It seems that session timed out but error message is not user friendly. After getting this error, are you able to access any page in Salesforce or redirected to login page ?

      1. Sakthivakeswaran Avatar
        Sakthivakeswaran

        Yes sir. I could. Kindly let me know which session timeout you are specifying.? salesforce session timeout or remote action timeout?? because I tried to produce both error message. they responds with a different exception message.

      2. Sakthivakeswaran Avatar
        Sakthivakeswaran

        Yes sir. I could. When session timeout happens, i think we get different messages. but this one is unusual.
        When i evaluated my page today i get another unusual error.

        ‘Syntax error’. Logged in? in the same alert box

      3. Sakthivakeswaran Avatar
        Sakthivakeswaran

        @JitendraZaa:disqus
        Yes sir. I could. When session timeout happens, i think I get different messages. but this one is unusual.
        When i evaluated my page today i get another unusual error.

        ‘Syntax error’. Logged in? in the same alert box

      4. Sakthivakeswaran Avatar
        Sakthivakeswaran

        @JitendraZaa:disqus

        Yes sir. I could. When session timeout happens, i think I get different messages. but this one is unusual.
        When i evaluated my page today i get another unusual error.

        ‘Syntax error’. Logged in? in the same alert box.

Leave a Reply to JitendraZaaCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading