LaceySnr.com - Salesforce Development Posts by Matt Lacey

When To Use Visualforce Remote Objects

Posted: 2016-02-02

Visualforce Remote Objects are an interesting feature of the Salesforce.com platform. On the one hand they're very powerful, and allow you to create comprehensive single-page applications without writing a line of Apex, on the other they can be rather slow, so you need to think carefully about where and when to use them.

When Not To Use Them

If you want to search hundreds of thousands of rows of data in an object Visualforce Remote Objects are not going to cut it: take it from me, I learned this the hard way. I was trying to do the equivalent of a SOQL LIKE clause on 600,000 contacts and the request was timing out at 30 seconds.

Switching to a regular remoting call that leveraged SOQL (albeit without any CRUD/FLS checks) the request was being handled in a little over a second, and switching to the correct tool for the job, SOSL, the request was handled in under a second.

When They Rock

If you're building a Visualforce page with a dynamic UI and want to get data in and out of the system easily with JavaScript, then Visualforce Remote Objects could be your best friend. If you're dealing with single records or small collections of records then I really can't think of a more convenient way of dealing with the backend of the platform.

Screenshot of the Proximity Insight Clienteling Calendar
Our Proximity Insight Clienteling Application still uses Visualforce Remote Objects for areas where we know the record counts aren't going to make performance an issue

Not only do you not need any Apex code (and therefore any test methods) you don't have to concern yourself with implementing security checks (because you always do that when working with Apex, right?), so there are some real tangible benefits to using them. If you've not played with them before and you are doing this kind of work then you really should check them out.

Quick Tip for Using Them With a Framework

Chances are if you do find yourself using Visualforce Remote Objects you'll be using some kind of front end framework like Angular, React or Knockout, and they do work well together.

One minor issue though is that sometimes you want a 'clean' object, i.e. an object with just the fields queried as properties and no extraneous methods and the like. Below is a simple code snippet that you can use to process a list of records returned by a Visualforce Remote Object query and convert each record into a basic JavaScript object with the shorthand names you've defined (see the example here) as properties.

function mapRemoteObjects(records)
{
    return records.map(function(record)
    {
        var obj = {};

        for(var f in record._fieldShorthands)
            obj[f] = record.get(f);

        if(record._props.Id)
            obj.Id = record._props.Id;

        return obj;
    });
}

In Short