---
schema_version: "1"
id: 60dca7d7afa3b264ee35bd87180381bd14008704590e28a2b6ca2415c0829fa7
title: Using Visualforce Remote Objects in JavaScript
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/javascript/pages-remote-objects-using.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-remote-objects-using.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: f6035c68e8d2aa9a4610f6f645e9506256026c939c4031de91c7b556418ea277
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# Using Visualforce Remote Objects in JavaScript

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:

```visualforce
<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:

```javascript
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.

```visualforce
<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.

```javascript
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:

```javascript
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:

```javascript
var ct = new RemoteObjectModel.Contact({ FirstName: "Aldo" });
ct.get('FirstName');  // 'Aldo'
ct.get('Phone'); // <undefined>
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()`.

