---
schema_version: "1"
id: a358fe16cf006d6e68370beff19ccaf6df3b78250750397245a6c403af86d830
title: Upserting Records with Remote Objects
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-upsert.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-remote-objects-using-upsert.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 28ec879518493d52825b8df5714704ef0f8920c2b8c22ad2332aca55781f418a
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# Upserting Records with Remote Objects

Save a record by calling `upsert()` on a Remote Objects model instance.

`upsert()` is a convenience function that updates a record if it exists and creates it if it doesn’t. Behind the scenes `upsert()` delegates to `create()` or `update()`. Use `upsert()` to write functions for your page or application that aren’t affected by whether a record is from a new input form or an edit record page.

`upsert()` accepts two arguments, both optional.

```javascript
RemoteObjectModel.upsert({field_values}, callback_function)
```

The `field_values` block enables you to set the values and save 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 `upsert()` are equivalent.

```javascript
// Call upsert() on a Contact model, with no arguments
// ct is a RemoteObjectModel.Contact that already has data
ct.set('Phone', '(415) 777-1212');
ct.upsert();

// Call upsert() on a Contact model, passing in field values
// ct is a RemoteObjectModel.Contact that already has data
ct.upsert({Phone: '(415) 777-1212'});
```

In the preceding example, it’s not clear if the contact exists in the database or if it’s a new contact that’s coming from an input form. `upsert()` handles the details. If there’s an `Id` field set on the contact, the contact will be updated. If there’s no `Id`, a new contact is created.

`upsert()` doesn’t return a result directly. The callback function enables you to handle the server response asynchronously.

**note:** All server operations that use Remote Objects are performed asynchronously. Any code that depends on the request being completed, including handling returned results, must be placed in the callback function.

Your callback function can accept up to three arguments.

```javascript
function callback(Error error, Array results, Object event) { // ... }
```

See [Visualforce Remote Objects Callback Functions](https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-remote-objects-callback-functions.md) for details about writing Remote Objects callback functions.

**See Also**  


-   [Creating Records with Remote Objects](https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-remote-objects-using-create.md)
-   [Updating Records with Remote Objects](https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-remote-objects-using-update.md)

