Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Upserting Records with
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.
1RemoteObjectModel.upsert({field_values}, callback_function)1// Call upsert() on a Contact model, with no arguments
2// ct is a RemoteObjectModel.Contact that already has data
3ct.set('Phone', '(415) 777-1212');
4ct.upsert();
5
6// Call upsert() on a Contact model, passing in field values
7// ct is a RemoteObjectModel.Contact that already has data
8ct.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.
1function callback(Error error, Array results, Object event) { // ... }