NodeJs Salesforce Integration using JSForce: A quick guide to look out in 2021 and beyond

Page 1

NodeJs Salesforce Integration using JSForce: A quick guide to look out in 2021 and beyond

www.bacancytechnology.com


Quick Summary: This post will help you understand how to integrate Salesforce with node.js using JSForce.


Introduction


Heya, welcome to the Salesforce community. The journey would be fantastic for sure. If you are new to the community, you may initially face some troubles, but the Salesforce community stands by its people to assist them in growing more robust and better.

Salesforce has already done wonderful things in a very short time. It has expanded its powerful presence in almost every sector. Every 3 out of 5 organizations are now adopting this excellent CRM for better customer experience and business operations.

Not only in the industries, but Salesforce is also doing wonders with frameworks. Node js salesforce integration is the current buzzword in the development market. Yes, Salesforce Nodejs is a real-time integration. If you are wondering, how? Let us explain to you with this blog.


This blog will guide you through the detailed salesforce integration with Node.js, explaining the concept, prerequisites, and workflows. Before starting our blog in detail, let’s look at how Salesforce with Node.js is practical.

We all know that JavaScript is the language of the web. Every major browser supports this language as primary for building clientside web apps. If you are even working with Typescript or another language, it is later compiled into JavaScript to run smoothly on the browser.

Even salesforce developers use JS to build applications; Javascript powers the components like Lightning web and AURA. Server-side applications like Salesforce CLI are also created using JavaScript.


And when it comes to Node.js, it is an opensource JS runtime powered by V8. But why should you go for node.js with Salesforce? Let us tell you why?

By using nodejs with Salesforce, you can

Build applications faster Improvement in the development process Build and compile lightning web components using Node.js


With Node.js, you can deploy beautiful, pixel-perfect web applications built with modern technologies and deploy highly scalable web services. It lets you create pro-code applications on the Salesforce Platform.

We hope these reasons are enough for you to understand how tremendous and beneficial nodejs salesforce integration is for your business. So let’s not waste any seconds and start learning Salesforce integration with Nodejs.

Nodejs with Salesforce can be integrated mainly with two mediums, including endpoint and JSForce. In this post, we discuss how to integrate Salesforce with nodejs using JSForce.

Without any further ado, let’s get started with what JSforce is.


What is JSforce and what makes it so popular?


JSforce (f.k.a. Node Salesforce) is an isomorphic JavaScript Library using Salesforce’s API: It works both in the program and with Node.js. It encapsulates the admittance to different APIs given by Salesforce in offbeat JavaScript work calls.

Dissimilar to other Salesforce API libraries, it is planned to provide a coordinated interface for both server-side and customer-side applications, so you don’t need to rework comparative rationales with various libraries just for running in multiple climates.

Furthermore, it has a helpful control line interface (CLI) which gives an intelligent control center (REPL), so you can gain proficiency with the utilization without issue. In short words, with the assistance of JSforce, We can get admittance to different APIs given by Salesforce in nonconcurrent JavaScript work calls.


Prerequisites to use JSforce with Salesforce


When talking about adopting the new technology, there are a few prerequisites that one needs to take care of. With some basic information working on anything new becomes quite exciting and engaging.

For JSforce, all you need to take care of

Install Node Js: Since the functionality would use the java library, installing Node JS in your system is necessary. Install JSforce: When talking about development for Salesforce using JavaScript then, JSforce comes into the picture. It makes the development process here easier and comprehensive. Install Express Js: With express JS, things become easier; all you need to do is focus on working both platforms together.


A Salesforce org.: Since we are talking here about the integration of Salesforce with the Jsforce, you would need a Salesforce org.

These are the essential requisites before you move forward with node js salesforce integration.


With Node js Salesforce Integration: What is the workflow?


When talking about workflow to integrate Salesforce with Node.js, the process is quite simple. This process makes it easier for the experts to work with both technologies at once.

First, a Node Js or JS file is created that makes it easier for the people.

Next, we need to include the Express framework in JS to reflect the information on the browser.

We’ll remember JSforce for the JS record for making association with Salesforce. We’ll support returned Data from Salesforce after running this js record.

We should begin:

1. First, we need to Download Node Js on your PC.


Go to https://nodejs.org/en/ Pick your OS Type Download and Install it. Ensure you have effectively installed it. Run this command- $ node -v in Terminal and check its adaptation.

2. After effectively installing Nodejs, now we need to introduce JSforce.

We will install it with the help of Node package manager (npm) Run this command in the terminal- $ npm introduce jsforce Install JSforce We’ve successfully installed JSforce. 3. Now we need to install Express framework

To install express, run this command in the terminal $ npm install jsforce


4. Now we need to create a JS file in order to connect with Salesforce First, we’ll establish this connection, and after that, we’ll perform some DML tasks in Salesforce; for authentication, we will utilize Username and Password+SecurityToken. Make a JS document in any code editorial manager; we will utilize VS Code. var express = require( 'express' ); //Adding Express var http = require( 'http' ); //Adding http var jsforce = require('jsforce'); //Adding JsForce var app = express(); app.set( 'port', process.env.PORT || 3001 ); app.get('/', function (req, res) {


var conn = new jsforce.Connection({ // you can change loginUrl to connect to sandbox or prerelease env. // loginUrl : 'https://test.salesforce.com' }); var username = 'xyz@salesforce.com'; var password = 'PassWord+SecurityToken'; conn.login(username, password, function(err, userInfo) { if (err) { return console.error(err); } // Now you can get the access token and instance URL information. // Save them to establish a connection next time. console.log(conn.accessToken); console.log(conn.instanceUrl); // logged in user property console.log("User ID: " + userInfo.id); console.log("Org ID: " + userInfo.organizationId); res.send('heySalesforce : JSForce Connect


Successed!'); }); }); http.createServer( app ).listen( app.get( 'port' ), function (){ console.log( 'Express server listening on port ' + app.get( 'port' )); });

Save the code with the .js extension. Run this command $ node myFirst.js, to run the server on local host We’ll get some details from Salesforce like AccessToken, UserID, Org ID, and console it. We have successfully created an association with Salesforce now.

Now it’s time for DML (Data Manipulation Language) operations


5. Now retrieve a few accounts and show them in terminal with the help of following code var records = []; conn.query("SELECT Id, Name FROM Account", function(err, result) { in the event that (blunder) { return console.error(err); } console.log("total : " + result.totalSize); console.log("fetched : " + result.records.length); });


This is really straightforward. We will utilize SOQL inquiry and show the all outnumber of Accounts in our organization. For that, we need to place the above code after effective authorization and getting access code in past code.

Simply view beneath code. var express = require( 'express' );//Adding Express var http = require( 'http' );//Adding http var jsforce = require('jsforce');//Adding JSforce var application = express(); app.set( 'port', process.env.PORT || 3001 ); app.get('/', work (req, res) {


var conn = new jsforce.Connection({

/you can change the login URL to interface with the sandbox or prerelease env.

/loginUrl : 'https://test.salesforce.com'

});

var username = 'asifjee@salesforce.com';

var secret phrase = 'PassWord+SecurityToken';

conn.login(username, secret phrase, function(err, userInfo) {

in the event that (blunder) { return console.error(err); }

/Now, you can get the entrance token and example URL data.


/Save them to set up association sometime later.

console.log(conn.accessToken);

console.log(conn.instanceUrl);

/signed in client property

console.log("User ID: " + userInfo.id);

console.log("Org ID: " + userInfo.organizationId);

/Perform SOQL Here

var records = [];

conn.query("SELECT Id, Name FROM Account", function(err, result) {

on the off chance that (blunder) { return console.error(err); }


console.log("total : " + result.totalSize);

console.log("fetched : " + result.records.length);

});

res.send('heySalesforce : JSForce Connect Successed!');

});

});

http.createServer( application ).tune in( app.get( 'port' ), work (){

console.log( 'Express server tuning in on port ' + app.get( 'port' ));

}); }


Save the above code and run this code $ node myFirst.js safely. We got a total of 54 accounts.

6. Now we create some other accounts using the BULK API.

var accounts = [ { Name : 'MyAccount 1', site : 'heySalesforce.org' }, { Name : 'MyAccount 2', site : 'heySalesforce.org' }, { Name : 'MyAccount 3', site : 'heySalesforce.org' }, { Name : 'MyAccount 3', site : 'heySalesforce.org' } ];


conn.bulk.load("Account", "embed", accounts, function(err, rets) { on the off chance that (fail) { return console.error(err); } for (var i=0; I < rets.length; i++) { on the off chance that (rets[i].success) { console.log("#" + (i+1) + " Accounts made effectively, id = " + rets[i].id); } else { console.log("#" + (i+1) + " mistake happened, message = " + rets[i].errors.join(', ')); } } });


In the above code, we have made an Account Array, and with the assistance of Bulk API, Inserted Accounts. For each record, we have comforted achievement or a mistake message. Presently we should put this code after Authentication. View underneath code.

View the below code


var express = require( 'express' );/Adding Express var http = require( 'http' );/Adding http var jsforce = require('jsforce');/Adding JSforce var application = express(); app.set( 'port', process.env.PORT || 3001 ); app.get('/', work (req, res) { var conn = new jsforce.Connection({ /you can change the login URL to interface with the sandbox or prerelease env. /loginUrl : 'https://test.salesforce.com' });


var username = 'asifjee@salesforce.com'; var secret key = 'PassWord+SecurityToken'; conn.login(username, secret key, function(err, userInfo) { on the off chance that (blunder) { return console.error(err); } /Now, you can get the entrance token and occurrence URL data. /Save them to set up association sometime later. console.log(conn.accessToken); console.log(conn.instanceUrl); /signed in client property


console.log("User ID: " + userInfo.id); console.log("Org ID: " + userInfo.organizationId); /Perform SOQL Here var records = []; conn.query("SELECT Id, Name FROM Account", function(err, result) { in the event that (fail) { return console.error(err); } console.log("total : " + result.totalSize); console.log("fetched : " + result.records.length); });


/Perform account inclusion var accounts = [ { Name : 'MyAccount 1', site : 'heySalesforce.org' }, { Name : 'MyAccount 2', site : 'heySalesforce.org' }, { Name : 'MyAccount 3', site : 'heySalesforce.org' }, { Name : 'MyAccount 3', site : 'heySalesforce.org' } ]; conn.bulk.load("Account", "embed", accounts, function(err, rets) { on the off chance that (blunder) { return console.error(err); }


for (var i=0; I < rets.length; i++) { on the off chance that (rets[i].success) { console.log("#" + (i+1) + " Accounts made effectively, id = " + rets[i].id); } else { console.log("#" + (i+1) + " mistake happened, message = " + rets[i].errors.join(', ')); } } }); res.send('heySalesforce : JSForce Connect Successed!');


}); }); http.createServer( application ).tune in( app.get( 'port' ), work (){ console.log( 'Express server tuning in on port ' + app.get( 'port' )); });


var accounts = [ { Name : 'MyAccount 1', site : 'heySalesforce.org' }, { Name : 'MyAccount 2', site : 'heySalesforce.org' }, { Name : 'MyAccount 3', site : 'heySalesforce.org' }, { Name : 'MyAccount 3', site : 'heySalesforce.org' } ]; conn.bulk.load("Account", "embed", accounts, function(err, rets) { on the off chance that (blunder) { return console.error(err); } for (var i=0; I < rets.length; i++) {


on the off chance that (rets[i].success) { console.log("#" + (i+1) + " Accounts made effectively, id = " + rets[i].id); } else { console.log("#" + (i+1) + " mistake happened, message = " + rets[i].errors.join(', ')); } } }); res.send('heySalesforce : JSForce Connect Successed!'); }); });


http.createServer( application ).tune in( app.get( 'port' ), work (){ console.log( 'Express server tuning in on port ' + app.get( 'port' )); }); Save the above document and safely run this code. $node myFirst.js After running this code, You’ll see the ID of recently made accounts. Now check Salesforce Org and take a look at Accounts. The accounts will be successfully created accounts.

Now, try it yourself.


The Final Words!


JavaScript has been making things easier for programmers ever since it was conceived. Working on solutions may seem a little tricky initially, but gradually you would start liking the way things work and the ease it offers. Over the years, it has become much easier to earn better with limited things.

So if you are looking forward to integrating Salesforce with JavaScript, then this guide is undoubtedly your go-to place. Or we can help you integrate it, connect with our expert salesforce integration team, and let us assist you in node js salesforce integration.


Thank You

www.bacancytechnology.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.