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 = ;
var conn =
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
;
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
records
;
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
;
// Multiple Records
conn
;
ii. retrieve - To fetch existing record(s)
// Single Record
conn
;
// Multiple Records
conn
;
iii. update - To update existing record(s)
// Single Record
conn
;
// Multiple Records
conn
;
iv. destroy - To delete existing record(s)
// Single Record
conn
;
// Multiple Records
conn
}
;
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nisheet Sharma
Nisheet is a Full Stack Developer (MEAN). He is familiar with C, C++, Java, Html, Css, JavaScript, MySql, MongoDb, AngularJs, NodeJs, ExpressJs.