The JavaScript models that are generated by the Remote
Objects components provide a JavaScript API to create functions for your app that read and save
values back to Salesforce. Use the base model that is created by the <apex:remoteObjects> component to instantiate
specific models for corresponding sObjects. Then use the specific models to perform actions
on their sObjects, such as retrieving, creating, updating, and deleting.
The base model for your Remote
Objects is created by the
<apex:remoteObjects>
component. The base model provides a pseudonamespace for Remote
Objects that you create with it. By default the base model is named
SObjectModel, but you can set the name by using the
jsNamespace attribute. Use different base
models to group related Remote
Objects along functional or package lines. For
example:
<apex:remoteObjects jsNamespace="MyCorpModels">
<apex:remoteObjectModel name="Contact" fields="FirstName,LastName"/>
</apex:remoteObjects>
<apex:remoteObjects jsNamespace="TrackerModels">
<apex:remoteObjectModel name="Shipment__c" fields="Id,TrackNum__c"/>
</apex:remoteObjects>
Specific Models
You don’t normally create a base model yourself but instead use the generated
base model as a factory for creating specific models. For example, with the above
declaration, instantiate a Contact model in JavaScript like
this:
var ct = new MyCorpModels.Contact();
Note
that
ct is a JavaScript model for the
Contact object, not a specific Contact record.
ct represents a specific object, Contact, and provides a connection between your
page’s JavaScript and the Salesforce service. ct can be used to perform the basic “CRUD”
operations—create, read, update, and delete—on contact objects in the
database.
In the following sections, examples are based on the following Remote
Objects declaration, which uses all three Remote
Objects components and shows how to add a custom field,
Notes__c,
with a “shorthand” name to make accessing it in JavaScript more
natural.
<apex:remoteObjects jsNamespace="RemoteObjectModel">
<apex:remoteObjectModel name="Contact" fields="Id,FirstName,LastName,Phone">
<apex:remoteObjectField name="Notes__c" jsShorthand="Notes"/>
</apex:remoteObjectModel>
</apex:remoteObjects>
This
declaration enables you to access five fields on Contact records.
Instantiating Models and Accessing Fields
Instantiate a model with or without field values set, depending on your intent.
Generally, you’ll set fields when you want to write changes to the database
and omit fields when you’re just reading. Field values are set by passing in
a JSON string with values for the fields to set on the new model.
To create a model without fields set, create it with an empty parameters
list.
var ct = new RemoteObjectModel.Contact();
To instantiate a model with fields set, typically to create a new record, pass in an
object that contains field name and value pairs. For
example:
var ct = new RemoteObjectModel.Contact({
FirstName: "Aldo",
LastName: "Michaels",
Phone: "(415) 555-1212"
});
Remote
Objects models use basic
get() and
set() methods to retrieve and set field values.
For
example:
var ct = new RemoteObjectModel.Contact({ FirstName: "Aldo" });
ct.get('FirstName');
ct.get('Phone');
ct.set('FirstName', 'Benedict');
ct.set('Phone', '(415) 555-1212');
There’s
no functional difference between setting field values with a properties list in the
constructor and setting field values with
set().