Getting started with Visualforce Remote Objects

G

Visualforce Remote Objects are the proxy objects that ease your development by allowing you to create instances of standard/custom salesforce objects and allow DML operations from javascript itself.

Some awsome features Visualforce Remote Objects offers over JavaScript remoting are :

  • Freedom from controllers
  • No Test classes
  • Easy code management
  • Not counted against API call

 

Following Visualforce Page code demonstrate the concept.

<apex:page >

<!– Remote Objects definition –>
<apex:remoteObjects >
<apex:remoteObjectModel name=”Merchant__c” jsShorthand=”Merchant” fields=”Name,Id”>
<apex:remoteObjectField name=”Address__c” jsShorthand=”Address”/>
</apex:remoteObjectModel>
</apex:remoteObjects>

<!– JavaScript for Remote Objects calls –>
<script>
var fetchmerchant = function()
{
// Create a new Remote Object
var mer = new SObjectModel.Merchant();

// Use the Remote Object to query merchant records
mer.retrieve({ limit: 10 }, function(err, records, event)
{
if(err)
{
alert(err.message);
}
else
{
var ul = document.getElementById(“merchantList”);
//Loop to traverse through each recoard.
records.forEach(function(record)
{

var merText = “Merchant ”
merText += record.get(“Name”);
merText += ” is in “;
merText += record.get(“Address”);

// Add the line item to merchants list
var li = document.createElement(“li”);
li.appendChild(document.createTextNode(merText));
ul.appendChild(li);
});
}
});
};
</script>

<h1>Retrieve merchants via Remote Objects</h1>

<p>merchants :</p>

<ul id=”merchantList”>
</ul>
<button onclick=”fetchmerchant()”>Retrieve merchants</button>

</apex:page>

About the author

Krishna Kabra
By Krishna Kabra

Category