You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Accessing Salesforce S’Object as Database using JSForce


By: Radha

Now days, most of the Telecommunication companies need to store and access millions of records, thousands of users with a responsive Single Page Application (SPA) and the user did not have salesforce prior knowledge, so here we took new concepts from JavaScript framework called as JSFORCE

  1. JS Force known as npm-Salesforce it exists between structural JavaScript framework and Salesforce API.
  2. With the help of JSFORCE, we can access Salesforce sObjects (like database) to store, retrieve, and modify millions of records without prior Salesforce knowledge and an individual Salesforce user account.
  3. It is a light-weight framework that can do the all operations that the Apex code can do.
  4. Possible to develop Single Page Application with good UI experience.
  5. Developers can easily implement and get quick response from application. The framework deals with server API calls and reduces multi-page application to a Single Page Application(SPA).
  6. Single Page Application used to load (dynamically) entire application views into the single page.
  7. It can be easily deployed into the Heroku.
  8. End user doesn’t need Salesforce knowledge to create new record in Salesforce. We just need to develop an SPA application with SObject (Like Contact, Account, Opportunity) Object fields, meta data.
  9. This framework does not need any special force.com IDE or editor. It can run with editor like sublime text, notepad++, visual-studio code. They are all open-source and cost-effective too.
  10. It doesn’t need any deployment tool to deploy our new changes on the server. JSFORCE has used with proxy server; so, it directly affects application.

SUPPORTED API

JSFORCE supports the following API:

  1. REST API
  2. APEX REST
  3. Analytics API
  4. Bulk API
  5. Metadata API
  6. Streaming API
  7. Tooling API

CONFIGURE JSFORCE

When want to implement JSFORCE in our Single Page node application or other web application, there are two ways to do it.

  • Node Package.
  • JQuery CDN.

In node package, use this command.

$ npm install jsforce.

  • var jsConnect = require(‘jsforce’) used to connect Salesforce with jsforce.
  • For JQuery use CDN Download or use the following script to install JSFORCE in application. Use the html <script> tag to configure.

Example:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/force-js/0.1.1/force.min.js"></script> this link has all in one library.


var jsConnect = require('jsforce');

var connect = new jsConnect.Connection();

connect.login('username', 'password', function (err, res) {

if (err) {

return console.log('Error :', err);

}

connect.query('SELECT Id,FirstName, LastName FROM Contact',

function (err, result) {

if (err) {

alert(err);

return console.error(err);

}

alert('Total No of Records in Contacts is :' + result.totalSize);

});

});

REST API:

Basic character of REST API in Salesforce is it provides powerful and simple web service API to interact with force.com. So, the same process followed by the JSFORCE is managed by these: CRUD, SOQL, SOSL.

CRUD – CREATE, READ, UPDATE, DELETE

Operations: Retrieve records from sObjects, create Records into sObjects, update records to sObjects, and remove records from sObjects, so these are the operations performed inside theCRUD.

GET RECORD:

We can retrieve the records from sObjects records – either a single record or multiple records at a time.

Example

connect.sobject("Account").retrieve([

"id1",

"id2"

],

function (err, acc) {

if (err) {

alert(err);

}

for (var i = 0; i < acc.length; i++) {

alert(acc[i].Name);

}

});

CREATE RECORD:
Next important process in REST API is creating record. It helps to create new records in to the sObject.CREATE RECORD:

So, JSFORCE libraries executes with simple code.

Example:

connect.sobject("Account").create(

{

Name: 'Test Account',

AccountNumber: 'ACC-001'

}, function (error, result) {

if (error) {

alert(error);

}

else {

alert(result.id);

}

});

UPDATE RECORDS:

This is another operation in the CRUD to update records.

Example:

connection.sObject("Contact").Update({

Id = "", Name = ""

}, function (error, result) {

if (error) {

alert(error)

} else {

alert("Given contact has been updated" + result.id);

}

});

REMOVE RECORD:

Another basic operation in the CRUD is deleting records. It uses del or destroy annotations to remove the records.

Example:

Connetion.sObject("Contact").del(["id1"

, "id2"], function (error, result) {

if (error) {

alert(error);

}

else {

for (var i = 0; i < result.lenght; i++)

if (result[i].success)

alert("Record removed successfully");

}

});

UPSERT RECORD:

Here, we can also do UPSERT operation—either inserting a new record or updating an existing record.

Example:

Connection.sObject("Account").upsert({

    Id = "", Name = "",

}, "Id", function (error, result) {

    if (error) {

        alert(error);

    }

    else {

        alert("Record Upserted Successfully");

    } });

Conclusion:

This JavaScript framework is used to perform simple CRUD operations with very simple client side scripting, and it can perform bulk data insert and view data. This client side scripting reduces workload of servers and makes the whole loading process a light-weight process.