Develop Salesforce apps with JavaScript,
whether it's running on server or web browser.

Download View on GitHub

Setup

$ npm install jsforce

Example

const jsforce = require('jsforce');

const conn = new jsforce.Connection();
await conn.login('username@domain.com', 'password')

const res = await conn.query('SELECT Id, Name FROM Account')
console.log(res)

Example

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/jsforce@3.2.2/dist/jsforce.min.js"></script>
  <script>
    jsforce.browser.init({
      clientId: '[ your Salesforce OAuth2 ClientID is here ]',
      redirectUri: '[ your Salesforce registered redirect URI is here ]'
    });
    jsforce.browser.on('connect', async function(conn) {
      const res = await conn.query('SELECT Id, Name FROM Account')
      console.log(res);
    });
  </script>
</head>
<body>
  <button onclick="javascript:jsforce.browser.login();">Login</button>
</body>
</html>

Example

<apex:page docType="html-5.0" showHeader="false">
  <apex:includeScript value="{!URLFOR($Resource.JSforce)}" />
  <script>
    const conn = new jsforce.Connection({ accessToken: '{!$API.Session_Id}' });
    const res = await conn.query('SELECT Id, Name FROM Account');
    console.log(res);
  </script>
</apex:page>

Example

<!DOCTYPE html>
<html>
<head>
  <meta id="sf-canvas-signed-request" content="<%= verifiedSignedRequestJSON %>" />
  <script src="https://login.salesforce.com/canvas/sdk/js/42.0/canvas-all.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/jsforce@3.2.2/dist/jsforce.min.js"></script>
  <script>
    let sr = document.getElementById('sf-canvas-signed-request').content;
    const conn = new jsforce.Connection({ signedRequest: sr });
    const res = await conn.query('SELECT Id, Name FROM Account');
    console.log(res);
  </script>
</head>
<body>
</body>
</html>

Setup

$ npm install -g jsforce

Example


$ jsforce
> .connect username@salesforce.example.org
Password: *******
Logged in as : username@salesforce.example.org
> query("SELECT Id, Name FROM Account LIMIT 1")
{ totalSize: 1,
  done: true,
  records: 
   [ { attributes: [Object],
       Id: '0015000000KBQ5GAAX',
       Name: 'GenePoint' } ] }
> .exit

$ jsforce -c username@salesforce.example.org -e "query('SELECT Id, Name FROM Account LIMIT 2')"
{"totalSize":2,"done":true,"records":[{"attributes":{"type":"Account","url":"/services/data/v29.0/sobjects/Account/001i0000009PyDrAAK"},"Id":"001i0000009PyDrAAK","Name":"GenePoint"},{"attributes":{"type":"Account","url":"/services/data/v29.0/sobjects/Account/001i0000009PyDsAAK"},"Id":"001i0000009PyDsAAK","Name":"United Oil & Gas, UK"}]}