Queries and CRUD Operations on Salesforce Records in NodeJs

Posted By Nisheet Sharma | 24-Jul-2018

In this blog, we'll see how to perform Queries and CRUD operations on Salesforce Records using the node-salesforce module which is a Salesforce API connection library, for Node js applications. 
To install it using npm use the following command,
npm install node-salesforce

Lets assume you have already logged into salesforce and have the access token and instance url shared by Salesforce.

First, setup the connection, as follows:

var sf = require('node-salesforce');
var conn = new sf.Connection({
  instanceUrl : '<your Salesforce server URL (e.g. https://na1.salesforce.com) is here>',
  accessToken : '<your Salesforce OAuth2 access token is here>'
});

1. Queries using SOQL :

Let's assume we have a Salesforce Object named Account, and we want to fetch the id and name of all records. To achieve this, we can write a basic SOQL query as shown below. (SOQL Stands for Salesforce Object Query Language).

var records = [];
conn.query("SELECT Id, Name FROM Account", function(err, result) {
  if (err) { return console.error(err); }
  console.log("total : " + result.totalSize);
  console.log("fetched : " + result.records.length);
});

This is the callback way to retrieve the results. To achieve the same result in an event-driven way, we can do the following:

var records = [];
conn.query("SELECT Id, Name FROM Account")
  .on("record", function(record) {
    records.push(record);
  })
  .on("end", function(query) {
    console.log("total in database : " + query.totalSize);
    console.log("total fetched : " + query.totalFetched);
  })
  .on("error", function(err) {
    console.error(err);
  })
  .run({ autoFetch : true, maxFetch : 4000 });

When the query is executed, it emits a record event for each record that matches the query string's conditions. Once the query is finished executing, it emits an end event. In case any error is encountered while processing the query, an error event is fired.

2. CRUD Operations :

node-salesforce provides basic CRUD operations for all the records in Salesforce. You can use it manipulate multiple records at the same time.
Let's say we want to perform CRUD operations on our salesforce object Account.
For this we can use the following functions:

i. create - To create new record(s)

// Single Record 
conn.sobject("Account").create({ Name : 'My Account #1' }, function(err, record) {
  if (err || !record.success) { return console.error(err, record); }
  console.log("Created record id : " + record.id);
  // ...
});

// Multiple Records
conn.sobject("Account").create([
  { Name : 'My Account #1' },
  { Name : 'My Account #2' }
],
function(err, records) {
  if (err) { return console.error(err); }
  for (var i=0; i < records.length; i++) {
    if (records[i].success) {
      console.log("Created record id : " + records[i].id);
    }
  }
  // ...
});

ii. retrieve - To fetch existing record(s)

// Single Record
conn.sobject("Account").retrieve("record_id_here", function(err, account) {
  if (err) { return console.error(err); }
  console.log("Name : " + account.Name);
  // ...
});

// Multiple Records
conn.sobject("Account").retrieve([
  "record_1_id_here",
  "record_2_id_here"
], function(err, accounts) {
  if (err) { return console.error(err); }
  for (var i=0; i < accounts.length; i++) {
    console.log("Name : " + accounts[i].Name);
  }
  // ...
});

iii. update - To update existing record(s)

// Single Record
conn.sobject("Account").update({ 
  Id : 'record_id_here',
  Name : 'Updated Account #1'
}, function(err, record) {
  if (err || !record.success) { return console.error(err, record); }
  console.log('Updated Successfully : ' + record.id);
  // ...
});

// Multiple Records
conn.sobject("Account").update([
  { Id : 'record_1_id_here', Name : 'Updated Account #1' },
  { Id : 'record_2_id_here', Name : 'Updated Account #2' }
],
function(err, records) {
  if (err) { return console.error(err); }
  for (var i=0; i < records.length; i++) {
    if (records[i].success) {
      console.log("Updated Successfully : " + records[i].id);
    }
  }
});

iv. destroy - To delete existing record(s)

// Single Record
conn.sobject("Account").destroy('record_id_here', function(err, record) {
  if (err || !record.success) { return console.error(err, record); }
  console.log('Deleted Successfully : ' + record.id);
});

// Multiple Records
conn.sobject("Account").destroy([
  'record_1_id_here',
  'record_2_id_here'
}], 
function(err, records) {
  if (err) { return console.error(err); }
  for (var i=0; i < records.length; i++) {
    if (records[i].success) {
      console.log("Deleted Successfully : " + records[i].id);
    }
  }
});
Request for Proposal

Recaptcha is required.

Sending message..