Use Visualforce Remoting in Salesforce

  • Visualforce Remote Objects is an effective tool for quickly adding simple data operations to Visualforce pages.
  • Visualforce Remote Objects are proxy objects that allow basic DML operations on sObjects directly from JavaScript.
  • The Remote Objects controller handles sharing rules, field-level security, and other data accessibility concerns. Pages that use Remote Objects are subject to all the standard Visualforce limits.
  • Like JavaScript remoting, Remote Objects calls don’t count towards API request limits.
  • Perform actions like Retrieve, Create, Update, Upsert, Delete with Remote Objects.

Visualforce Tags:

apex:remoteObjects : Use this component, along with child apex:remoteObjectModel and apex:remoteObjectField components, to specify the sObjects and fields to access using Visualforce Remote Objects. These components generate models in JavaScript that you can use for basic create, select, update, and delete operations in your client-side JavaScript code.

apex:remoteObjectModel : Defines an sObject and its fields to make accessible using Visualforce Remote Objects. This definition can include a shorthand name for the object, which you can use in JavaScript instead of the full API name. This is especially useful if your organization has a namespace or if you’re packaging an app, and makes your code more maintainable.

apex:remoteObjectField : Defines the fields to load for an sObject. Fields defined using this component, instead of the fields attribute of apex:remoteObjectModel, can have a shorthand name, which allows the use of a “nickname” for the field in client-side JavaScript code, instead of the full API name. Use as a child of apex:remoteObjectModel.

Here is an example of Visualforce remoting in Salesforce

Visualforce Page:

<apex:page >
    <!-- This Demo will assume Querying Account Object -->
    <apex:remoteObjects >
        <apex:remoteObjectModel name="Account" jsShorthand="getActs" fields="Name,Phone,Industry,Id,ParentId">
        </apex:remoteObjectModel>
    </apex:remoteObjects>
    
    <script>
    var src = new SObjectModel.getActs();
    
    //Use Remote Object to query 5 records
    src.retrieve({
        limit : 10,
    } ,
                 function(err,records){
                     if(err == null)
                     {
                         {
                             //Process returned "records" to display in Visualforce code.
                             //console.log(JSON.stringify(records));
                             var ul = document.getElementById("Account List");
                             records.forEach(function(record) {
                                 //console.log(JSON.stringify(records));
                                 // Build the text for a warehouse line item
                                 var whText = record.get("Id");
                                 whText += " -- ";
                                 whText += record.get("Name");
                                 whText += " -- ";
                                 whText += record.get("Phone");
                                 whText += " -- ";
                                 whText += record.get("Industry");
                                 
                                 // Add the line item to the warehouses list
                                 var li = document.createElement("li");
                                 li.appendChild(document.createTextNode(whText));
                                 ul.appendChild(li);
                             });
                         }
                         
                         
                         
                     }
                 } );
    
    </script>
    <h1>Retrieve Account Details via Remote Objects</h1>
    
    <p>Account Details:</p>
    
    <ul id="Account List">
    </ul>
    
</apex:page>

Output:

Advantages of Using Remote Objects :

  • Apex Controller is not required
  • No need for @RemoteAction methods(@RemoteAction annotated methods need to be static so you had to take special precaution as it didn’t support View state. This hurdle is completely removed now.)
  • Do not Consume Orgs daily API limits.
  • No need for test classes.
  • A light way to perform rapid client-side querying.

Limitations:

  • Remote Objects isn’t a way to avoid Salesforce service limits. Remote Objects calls aren’t subject to API limits, but Visualforce pages that use Remote Objects are subject to all standard Visualforce limits.
  • You can retrieve a maximum of 100 rows in a single request. To display more rows, submit additional requests by using the OFFSET query parameter.
  • Remote Objects doesn’t support Blob fields. You can’t retrieve or set the value of object fields of type Blob.
  • Setting the rendered attribute to false on Remote Objects components disables the generation of the JavaScript for those Remote Objects. Any page functionality that depends on unrendered Remote Objects should also be disabled.