Why Visualforce Remote Objects?
Salesforce recently rolled out Visualforce Remote Objects as a way to allow CRUD operations to Salesforce objects in a Visualforce Page without having to write code in a server-side controller. While I don’t have a problem writing code in a controller to handle CRUD actions on an object, if I could skip that step, then why wouldn’t I?
Visualforce Remote Objects Setup
We need to set up remote models for getting both the main objects and the parent objects. In my case, I’m using the standard Salesforce objects Contact as the main object and Account as the parent object. I’ve assigned the fields attribute with the list of fields that I’ll be using on this page. As a little something special, my jsShorthand names are actually longer than the original object names. Don’t judge.
Nothing fancy here. Functionally, I’ve got a filter textbox, a button, and an empty list that will hold my results. Obviously in a real implementation, you’d take the time to design a visually impressive page. Alright, let’s move on to the fun stuff.
Retrieve Primary Objects
Retrieve Parent Objects
This is the money code (never heard anybody use that phrase, but it could completely catch on). In loadAccountsForRetrievedContacts() , I’m getting all the parent Accounts for the Contacts we just retrieved. Essentially, I’m just building a list of all the Contact’s AccountId values. The part that took me the longest to get this working was figuring out that the In operator doesn’t like duplicate values in the list, so I’m filtering out any duplicate AccountId values. When the parent Accounts are returned, we hand control over to displayContacts() , which now has access to both the cached Contacts and Accounts.
If you’ve used Remote Objects in the past, this should look familiar. There are really only a couple lines different than normal. In the foreach Contact loop, the first thing we’re doing is to get the parent Account for the Contact using getRetrievedAccountRecordById() . In case you skipped over the name of the function, its only purpose is to find the Account using the Contact’s AccountId value. At this point, we have both the Contact record and the parent Account record available to do whatever we want to with them. Out of all the amazing things I could do with this, I chose to get the Account name. Boom!
Not That Hard
After reading through, you’re probably feeling kind of ripped off because I didn’t really do anything you couldn’t have already done on your own. True, but when I was searching for answers on how to get parent Visualforce Remote Objects, this would have at least given me a head start. From what I’ve seen, Salesforce is planning to make further improvements across their Remote Objects technology, so it wouldn’t surprise me if the approach I’ve taken is completely outdated in a couple releases. Good luck, and if you have any lessons learned as you’re using this technique, please post a comment so we can all benefit.