Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
179 lines (152 sloc) 4.01 KB

node-salesforce -- Connecting Salesforce via REST Api

Abstract

Node-salesforce is designed to be a wrapper of salesforce REST API from Node.js, which enables event-driven style development. It doesn't include authentication module, i.e light-weight. It works both on OAuth2 access token and session Id from SOAP API.

Install

  npm install node-salesforce

or

  git clone git://github.com/stomita/node-salesforce.git 
  cd node-salesforce
  npm link

Create Connection by giving OAuth2 Token

var sf = require('node-salesforce');
var conn = new sf.Connection({
  serverUrl : 'https://na1.salesforce.com',
  accessToken : '<your oauth2 access token is here>'
});

Create Connection and Login via SOAP API

var sf = require('node-salesforce');
var conn = new sf.Connection({
  loginUrl : 'https://login.salesforce.com'
});
conn.login(username, password, function(err) {
  if (!err) {
    // console.log(conn.accessToken);

    //
    // ... logics after authentication ...
    //
  }
});

Query Records by Event-Driven Style

var records = [];
conn.query("SELECT Id, Name FROM Account");
  .on("record", function(record) {
    records.push(record);
  })
  .on("end", function(query) {
    console.log("total in database : " + query.totalSize);
    console.log("total fetched : " + query.totalFetched);
  })
  .run({ maxFetch : 4000 });

Query Records by Callback Style

var records = [];
conn.query("SELECT Id, Name FROM Account", function(err, result) {
  if (!err) {
    console.log("total : " + result.totalSize);
    console.log("fetched : " + result.records.length);
  }
});

Retrieve Records by Record ID(s)

conn.sobject("Account").retrieve("0017000000hOMChAAO", function(err, account) {
  if (!err) {
    console.log("Name : " + account.Name);
  }
});
// Multiple records retrieval consumes one API request per record.
// Be careful for the API quota.
conn.sobject("Account").retrieve(["0017000000hOMChAAO", "0017000000iKOZTAA4"], function(err, accounts) {
  if (!err) {
    for (var i=0; i<accounts.length; i++) {
      console.log("Name : " + accounts[i].Name);
    }
  }
});

Create Record(s)

conn.sobject("Account").create({ Name : 'My Account #1' }, function(err, ret) {
  if (!err && ret.success) {
    console.log("Created record id : " + ret.id);
  }
});
// Multiple records creation consumes one API request per record.
// Be careful for the API quota.
conn.sobject("Account").create([{
  Name : 'My Account #1'
}, {
  Name : 'My Account #2'
}], 
function(err, rets) {
  if (!err) {
    for (var i=0; i<rets.length; i++) {
      if (rets[i].success) {
        console.log("Created record id : " + rets[i].id);
      }
    }
  }
});

Update Record(s)

conn.sobject("Account").update({ 
  Id : '0017000000hOMChAAO',
  Name : 'Updated Account #1'
}, function(err, ret) {
  if (!err && ret.success) {
    console.log('Updated Successfully : ' + ret.id);
  }
});
// Multiple records modification consumes one API request per record.
// Be careful for the API quota.
conn.sobject("Account").update([{
  Id : '0017000000hOMChAAO',
  Name : 'Updated Account #1'
}, {
  Id : '0017000000iKOZTAA4',
  Name : 'Updated Account #2'
}], 
function(err, rets) {
  if (!err) {
    for (var i=0; i<rets.length; i++) {
      if (rets[i].success) {
        console.log("Updated Successfully : " + rets[i].id);
      }
    }
  }
});

Delete Record(s)

conn.sobject("Account").del('0017000000hOMChAAO', function(err, ret) {
  if (!err && ret.success) {
    console.log('Deleted Successfully : ' + ret.id);
  }
});
// Multiple records deletion consumes one API request per record.
// Be careful for the API quota.
conn.sobject("Account").destroy([ // synonym of "del"
  '0017000000hOMChAAO',
  '0017000000iKOZTAA4'
}], 
function(err, rets) {
  if (!err) {
    for (var i=0; i<rets.length; i++) {
      if (rets[i].success) {
        console.log("Deleted Successfully : " + rets[i].id);
      }
    }
  }
});
Something went wrong with that request. Please try again.