Introduction to Node.js and Salesforce

Red Argyle logo

node.js and salesforceIn the world of software development, it’s hard to go anywhere lately without hearing people talking about Node.js. For those unfamiliar with Node.js, it is a platform which allows the JavaScript programming language to be used in building web and desktop applications. JavaScript has traditionally be used primarily as something that runs in your web browser to enable websites to be dynamic and interactive, however the development of Node.js has allowed for web applications to be completely built on JavaScript. Now, instead of having a web application that runs Ruby on Rails, Python, or PHP on the server, JavaScript can power both the server-side and client-side of the application. This leads to fast and scalable development as developers are only required to work in one language and libraries can be used on both the server- and client-side. To top it off, Node.js uses a fantastic asynchronous model which makes building modern, real-time network applications easy.

Connecting Node.js and Salesforce

We’ve been using Node.js at Red Argyle for about a year now, and being a Salesforce partner we are always looking for new tools to help with our Salesforce integration projects. I found a handful of libraries that can connect Node.js to the Salesforce API, but I took a particular liking to JSforce. JSforce seems to work with nearly all of the Salesforce APIs, including the REST API, Chatter API, Bulk API, and Tooling API. The user documentation is excellent and allowed me to get up and running in no time. The feature that really peaked my interest is that JSforce can be used both in Node.js and in the web browser, which coincides perfectly with the advantages of Node.js itself.

Node.js and salesforceAn especially exciting feature of JSforce is that it allows for an application to connect to a Salesforce Org via OAuth. OAuth is a secure method for consumers to authorize an application to access their account. In this case, a consumer could give authorization for a Node.js web application to access and interact with their Salesforce Org. This model is safe for the consumer because they can easily revoke the application’s access to their account at any time through Salesforce (My Settings -> Personal -> Connections).


Get Started with Node.js and JSforce

Below is some boilerplate code I’ve put together to get you up and running quickly with Node.js and JSforce. Note that the code uses Express.js which is a web application framework for Node.js. Express.js is not required for Node.js + JSforce functionality, but it offers a great set of features for quickly building a Node.js application. There are tons of tutorials out there for getting up and running with Express.js, so I won’t go into that here.

node.js and salesforceFirst, define your JSforce OAuth object. The clientId and clientSecret are for your App, which must be registered in Salesforce (Setup -> Create -> Apps -> New Connected App). I suggest not hardcoding these, but instead saving them as environment variables. If you’re deploying to Heroku, you can store them at ‘process.env.SF_CLIENT_ID’ for example.

[gist id=”632d5c3cacd07f054968″ file=”new_jsforce_oauth”]

Next you create your OAuth routes. The first one will simply redirect the user to Salesforce to login and accept authorization. The second is the callback route which Salesforce will redirect the user to after authorization (this must be the same as the ‘redirectUri’ field above as well as the ‘Callback URL’ field you set in Salesforce when registering your app). Upon authorization, the user’s OAuth access token and instance URL will be saved to their session at ‘req.session.accessToken’ and ‘req.session.instanceUrl’. The user will then be redirected.

[gist id=”4b0fe6f0e7b12123dcba” file=”sf_oauth_routes”]

Authorization was successful and the app can now access the user’s Org. Below is a quick example showing the use of the access token received above to query the user’s Salesforce Org through JSforce. The resulting records are passed to a template engine for rendering.

[gist id=”d0ea66829f80c2d63d2d” file=”sf_accounts_query”]

Do you use Node.js? What are your tips for other Node.js users? If you have comments, suggestions, or questions be sure to leave them below.

Red Argyle logo
Red Argyle logo

Related Blog Posts