Skip to content

Instantly share code, notes, and snippets.

@shivanathd
Forked from dcarroll/gist:8427394
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivanathd/ccfde0b2bd68257109ff to your computer and use it in GitHub Desktop.
Save shivanathd/ccfde0b2bd68257109ff to your computer and use it in GitHub Desktop.
/* PRE - model declaration on vf page
<apex:remoteObjects>
<apex:remoteObjectModel name="Account" fields="Id,Name" />
</apex:remoteObjects>
*/
/* INSTANCE */
// 1. W/o default properties
var acc = new SObjectModel.Account();
// 2. W/ default properties
var acc = new SObjectModel.Account({ Name: 'Blah' });
/* GETTER/SETTER */
// can use full API name of field or shorthand */
acc.set('Name', 'Blah');
acc.get('Name');
/* CREATE */
// 1. No arguments - typically used with a instance with properties
var acc = new SObjectModel.Account({ Name: 'Blah' });
acc.create(); // NOTE: all CRUD methods are async
// 2. W/ properties
var acc = new SObjectModel.Account();
acc.create({ Name: 'Blah' }); // NOTE: all CRUD methods are async
// 3. W/ callback
var acc = new SObjectModel.Account({ Name: 'Blah' });
acc.create(function(err) {
if (err) alert(err);
else alert(acc.get('Id'));
});
// 4. W/ properties & callback
var acc = new SObjectModel.Account();
acc.create({ Name: 'Blah' }, function(err) {
if (err) alert(err);
else alert(acc.get('Id'));
});
/* RETRIEVE */
// 1. W/ criteria & callback
var acc = new SObjectModel.Account();
acc.retrieve({
where: {
Name: { eq: 'Blah' }
}
}, function(err, records) {
if (err) alert(err);
else alert(records.length);
});
// NOTES:
// Criteria can be empty as well ({})
// where can have operators: eq,gt,lt,and,or,like
// we also support limit and offset properties on criteria
// 2. W/ criteria function that returns the criteria object & callback
var acc = new SObjectModel.Account({ Name: 'Blah' });
acc.retrieve(function() {
var criteria = {
where: {
Name: { eq: this.get('Name') }
}
};
return criteria;
}, function(err, records) {
if (err) alert(err);
else alert(records.length);
});
// 3. In 190 ONLY we can also call retrieve without criteria, in which case it is auto-generated based on instance properties
// this behaves just like 1 or 2 above
var acc = new SObjectModel.Account({ Name: 'Blah' });
acc.retrieve(function(err, records) {
if (err) alert(err);
else alert(records.length);
});
/* UPDATE */
// 1. W/o any arguments
var acc = new SObjectModel.Account({
Id: '001xxxxxxxxxxxxxxx',
Name: 'Blah Blah'
});
acc.update(); // NOTE: all CRUD methods are async
// 2. W/ callback
var acc = new SObjectModel.Account({
Id: '001xxxxxxxxxxxxxxx',
Name: 'Blah Blah'
});
acc.update(function(err, ids) {
if (err) alert(err);
else alert(ids.length);
});
// 3. W/ id
var acc = new SObjectModel.Account({
Name: 'Blah Blah'
});
acc.update('001xxxxxxxxxxxxxxx'); // NOTE: all CRUD methods are async
// 4. W/ ids - bulk update
var acc = new SObjectModel.Account({
Name: 'Blah Blah'
});
acc.update(['001xxxxxxxxxxxxxxx', '001xxxxxxxxxxxxxxx']); // NOTE: all CRUD methods are async
// 5. W/ properties to update - preferable if only some properties are to be updated
var acc = new SObjectModel.Account({
Id: '001xxxxxxxxxxxxxxx'
});
acc.update({
Name: 'Blah Blah'
}); // NOTE: all CRUD methods are async
// 6. W/ id & properties
var acc = new SObjectModel.Account();
acc.update('001xxxxxxxxxxxxxxx', {
Name: 'Blah Blah'
}); // NOTE: all CRUD methods are async
// 7. W/ ids & properties
var acc = new SObjectModel.Account();
acc.update(['001xxxxxxxxxxxxxxx', '001xxxxxxxxxxxxxxx'], {
Name: 'Blah Blah'
}); // NOTE: all CRUD methods are async
// 8. W/ properties & callback
var acc = new SObjectModel.Account({
Id: '001xxxxxxxxxxxxxxx'
});
acc.update({
Name: 'Blah Blah'
}, function(err, ids) {
if (err) alert(err);
else alert(ids.length);
});
// 9. W/ ids, properties & callback
var acc = new SObjectModel.Account();
acc.update(['001xxxxxxxxxxxxxxx', '001xxxxxxxxxxxxxxx'], {
Name: 'Blah Blah'
}, function(err, ids) {
if (err) alert(err);
else alert(ids.length);
});
/* DELETE (del) */
// 1. W/o any arguments
var acc = new SObjectModel.Account({
Id: '001xxxxxxxxxxxxxxx'
});
acc.del(); // NOTE: all CRUD methods are async
// 2. W/ callback
var acc = new SObjectModel.Account({
Id: '001xxxxxxxxxxxxxxx'
});
acc.del(function(err, ids) {
if (err) alert(err);
else alert(ids.length);
});
// 3. W/ id
var acc = new SObjectModel.Account();
acc.del('001xxxxxxxxxxxxxxx'); // NOTE: all CRUD methods are async
// 4. W/ ids - bulk delete
var acc = new SObjectModel.Account();
acc.del(['001xxxxxxxxxxxxxxx', '001xxxxxxxxxxxxxxx']); // NOTE: all CRUD methods are async
// 5. W/ id & callback
var acc = new SObjectModel.Account();
acc.del('001xxxxxxxxxxxxxxx', function(err, ids) {
if (err) alert(err);
else alert(ids.length);
});
// 6. W/ ids & callback
var acc = new SObjectModel.Account();
acc.update(['001xxxxxxxxxxxxxxx', '001xxxxxxxxxxxxxxx'], function(err, ids) {
if (err) alert(err);
else alert(ids.length);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment