Retrieve Accounts and Create Account via Salesforce Remote Objects

In the Spring’14 release, Salesforce had announced the support for JavaScript with its popular Visualforce Remoting feature. Visualforce Remoting was introduced to ease database operations such as create, read, update and delete in Javascript without using Apex or counting API request limits.

Used for building web apps with Visualforce, JavaScript Remoting is a popular, powerful and efficacious method. It enables the users to create pages to use in Salesforce1 or work with JavaScript libraries like jQuery or AngularJS efficiently.

Visualforce Remote Objects

Visualforce Remote Objects helps in the basic DML operations on sObjects from the JavaScript. They are the proxy objects that ease the JavaScript Remoting by cutting down the use of @RemoteAction methods in an Apex controller or extension.

Visualforce Remote Objects consists of the following:

  • Access definitions: 

Using the new Remote Objects components, Access definitions are written in Visualforce and generate a set of JavaScript proxy objects.

  • Data access functions: 

Written in JavaScript, Data Access functions use the proxy objects that are made available by the access definitions to perform create, select, update, and delete operations on the data.

For Live Demo on Create Account Using Remote Object Click Here

For Live Demo on Retrieve Accounts Using Remote Object Click here

Using Remote Objects in JavaScript

Remote Object Components generate Javascript models that provide JavaScript API to create functions for the apps that can read and save values back to Salesforce. One of the remote object components is <apex:remoteObjects> component that creates the base model to instantiate specific models for corresponding sObjects. These specific models enable multiple actions such as retrieving, creating, updating, and deleting to be performed on their sObjects.

The name of the base model created is sObjectModel by default and can be changed later using the jsNamespace attribute.

Basic description of Visualforce Remote Objects:-

  • Enables the user to create pages with basic data operations without content the API request limits.

  • Its tags are more suitable for Salesforce1, jQuery and AngularJS.

  • Supports basic DML operations such as Create, Read, Update and Delete in javascript without writing even a single line if apex code.

  • Controller and test classes are not required to be written anymore as it can be achieved using the Visualforce page.

Limitations of Visualforce Remote Objects

  • Though it provides new capabilities, Visualforce Remote Objects is not a complete feature yet.
  • Remote Objects have limitations of their own as they are subject to change without providing backward compatibility.
  • Remote Objects enables you to retrieve a maximum of 100 rows only in a single request. To display more records, you need to submit an additional request by using the OFFSET query parameter.

Example:

<apex:remoteObjects>

<apex:remoteObjectModel name=”Account” jsShorthand=”account”    fields=”Name,Id,FirstName,LastName,Phone,Rating”>

              </apex:remoteObjectModel>

</apex:remoteObjects>

Detail description above example :-

  • <apex:remoteObjects> : Used to include everything related to remote objects.
  • <apex:remoteObjectModel name=”Account” jsShorthand=”account” fields=”name”>: Used to define the object on which the DML operations will be performed.
  • jsShortHand attribute: Defines the shortcut for the object notation that can be used inside the JavaScript code.
  • “fields” attribute: Used to refer the fields to add conditions in the DML operations.

Creating Records suing Remote objects:

  • create() is used to create a record on a Remote Object instance.

RemoteObjectModel.create({field values},  Callback function)

The field values block defines the record to create in one statement.

Example :-

var createRecord = { Name : ‘AAA’};

var getRecord = new RemoteModel.account();

getRecord.create(createRecord);

create() does not return a result directly. Callback function is used to handle the server response.

Retrieve Records using Remote Objects:

  • We can retrieve the record suing the retrieve() function in remote object.

var getRecord = new SobjectModel.account();

First we need to initialize the remote objects and it is applicable for our DML operation method.

Example:-

var getRecord = new SobjectModel.account();

getRecord.retrieve( { where : name : {‘AAA’}} );

retrieve() does not return a result directly. The callback function enables you to handle the server response.

Create Account via Remote Object

VisualForcePage:

Retrieve Account via Remote Object VisualForcePage:

Conclusion

  • Visualforce Remote Objects are the best and lightest way to perform rapid client-side querying without impacting API limits. Here is everything you need to know about Retrieve Accounts and Create Account via Remote Objects. Please feel free to reach out to us at sales@hicglobalsolutions.com should you have any queries in programming.

    Happy Learning!

Our Location worldwide
India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203117884
SR Tower 2nd Floor Hydel Gate Haldwani Uttarakhand 263126 +91-5946359996
USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada
HIC Global Solutions INC
43 Lafferty Lane, Richmond Hill, L4C 3N8, CA +1(262) 310-7818