Tuesday, February 17, 2015

Visualforce Remote Objects

JavaScript remoting is a popular, powerful, and efficient method for building web apps with Visualforce, especially for creating pages for use in Salesforce1, or working with JavaScript libraries like jQuery or AngularJS. Visualforce Remote Objects are proxy objects that allow basic DML operations on sObjects directly from JavaScript. Remote Objects take some of the complexity out of JavaScript remoting, by reducing the need for @RemoteAction methods in an Apex controller or extension. Like JavaScript remoting, Remote Objects calls don’ t count towards API request limits.

Using Visualforce Remote Objects consists of implementing two separate pieces of functionality on the same page.
• Access definitions, written in Visualforce using the new Remote Objects components. These components generate a set of JavaScript proxy objects that you can use in the next step.
• Data access functions, written in JavaScript. These functions use the proxy objects made available by the access definitions to perform create, select, update, and delete operations on your data.

Your page then uses the data access functions to respond to user interaction, including form submissions or controls changes, or in response to timers, or pretty much anything you can write in JavaScript.

Below are some sample example where Visualforce remote objects being used:
  • Create new record with Visualforce Remote Objects
Create a record by calling create() on a Remote Objects model instance.
create() accepts two arguments, both optional
RemoteObjectModel.create({field_values}, callback_function)

The field_values block enables you to define and create a record in one statement. Set field values as you do when you create a model, using a JSON string. For example, the following two calls to create() are equivalent. The callback function enables you to handle the server response asynchronously.

Below is code snippet:



  • Retrieve records from object with Visualforce Remote Objects
Retrieve records by calling retrieve() on a Remote Objects model instance.Retrieve() requires two arguments, one for query criteria and one for a callback handler.
RemoteObjectModel.retrieve({criteria}, callback_function)

criteria can be a Remote Objects query object or a function that returns one.

Below is code snippet:




Points to remember:

  • Remote Objects respects your organization’s field level security settings. Keep this in mind when you create pages that use Remote Objects. Fields that aren’t accessible to the person viewing the page appear blank. Actions that modify field data (create(), update(), and upsert()) fail with an error if they include inaccessible fields in the request.
  • Each Remote Objects operation (create(), update(), and so on) is a separate transaction. Each operation succeeds or fails on its own, which can be a problem when you need to create or modify multiple related objects as part of a business process. In contrast, JavaScript remoting transaction boundaries are on the Apex@RemoteAction method. It’s easy to create the invoice and related line-item records inside one method, where automatic Apex transactions ensure that all records are created together or not at all.
  • Remote Objects calls aren’t subject to API 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.
  • 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.
  • 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.