ForceJS 2 for simple Salesforce queries

Mitch Conquer
the painless business stack
2 min readMar 6, 2017

--

ForceJS 2 is a modern, modular library brought to you by Salesforce. It allows you to use the Salesforce REST APIs from a client-side JavaScript application. So if you are developing with a React app, you can use OAuth to authenticate with Salesforce and then query and modify data as you need.

This is an alternative option for writing a modern JavaScript app without VisualForce — but still grants the power for users to interact with Salesforce.

ES2015-compatible

ForceJS 2 is built on ECMAScript 2015 (aka ES6), which means you can import the library as modules and is also compatible with modern tooling.

Two features include:

Promises
Asynchronous actions all return native ES2015 promises and API calls are made using XML HTTP requests so there are no extra dependencies to bloat up the library.

Modules
You can also use ES2015 syntax to import individual modules rather than requiring the whole library in. It’s as easy as import OAuth from ‘forcejs/oauth’ .

ES5-compatible

ForceJS is also available as transpiled into an ECMAScript 5 version if you require more backwards compatibility.

<script src="force.all.js"></script">

var oauth = force.OAuth.createInstance();

// ...

</script>

Two Components Available Now

The library breaks down into two modules: OAth and Data-Service

OAuth
OAuth allows you to set up a connection with Salesforce using the User Agent workflow. If you just want a Salesforce access token, this module is all you need. It is pretty simple to set up and has the advantage of automatically renewing the access_token if it expires.

import OAuth from 'forcejs/oauth'

const appId = 'myAppId'

const loginURL = 'https://login.salesforce.com'

const oauthCallbackURL = 'https://www.example.com/loggedin.html'

const oauth = OAuth.createInstance(appId, loginURL, oauthCallbackURL)

oauth.login().then(

oauthResult => '...do something neato with result like instantiate DataService'

)

Data-Service
The data service module is where the magic happens. It allows you to retrieve and manipulate data from Salesforce APIs. After you have your access token, you can use it to create a DataService instance and access data on Salesforce.

You can either have a singleton instance or multiple named instances, whichever your app requires.

import { OAuth, DataService } from 'forcejs'

// 1.) Get your oauth

const appId = 'myAppId'

const loginURL = 'https://www.example.com/login'

const oauthCallbackURL = 'https://www.example.com/loggedin'

const oauth = OAuth.createInstance(appId, loginURL, oauthCallbackURL)

// 2.) Create instances of DataService

oauth.login().then(

oauthResult => DataService.createInstance(oauthResult)

).then(

() => {

const service = DataService.getInstance()

service.query('select id, Name from contact LIMIT 50')

.then(

response => {

let contacts = response.records

// have a ball with contacts

}

)

}

)

Go Nimbly is the premier marketing and sales consultancy for SaaS companies. Founded and headquartered in San Francisco, Go Nimbly provides customers with a customized team to manage everything from strategy to execution for their marketing and sales systems. To learn more, visit gonimbly.com.

--

--