GithubHelp home page GithubHelp logo

node-salesforce-api's Introduction

Node-Salesforce Integration

This is a sample for a Node-Salesforce integration. This allows you to hit both the SOQL and REST APIs exposed by Salesforce. For the SOQL queries, this allows the additional functionality of using SQL-like variables (as-in $1, $2, etc), protecting you from SQL injection.

Connection Authentication Overview

This exposed object works by hitting an exposed Salesforce OAuth Endpoint, which you must first create. After doing so, providing the CLIENT_SECRET and CLIENT_ID from your OAuth endpoint, along with your SALESFORCE_USERNAME and SALESFORCE_PASSWORD (in ensureConnected()) for your administrator account will allow requests to be made to Salesforce.

The ensureConnected() function that handles authentication between Node and Salesforce, retrieves an OAuth token, which is saved on the object, then used to authenticate all future requests. If the token ever becomes expired, the upper layers set the connection flag to false, forcing this function to re-authenticate and save the new token.

SOQL Query Usage

This is a very simple integration to use. You simply provide the query you'd like to execute in your Salesforce database, along with any params. For example, if you're looking to pull a specific Account, you can execute this query:

SELECT Name, Id FROM Account WHERE Id=$1

With the Id coming from the user. The whole implementation looks like this:

const sf = require('./node-salesforce-api')

const sfid = '1234567890'
const queryStr = 'SELECT Id, Name FROM Account WHERE Id = $1'

sf.sfConnection.query(queryStr, [sfid], function (error, accounts) {
  // ...
})

REST Action Usage

REST actions are similarlly easy to use -- this allows you to use CRUD to Create, Read, Update, or Destroy objects in Salesforce. This requires a method (GET, POST, PATCH, DELETE) and a resourceType (the name of the Salesforce Object) and optionally requires a resourceId and body.

CREATE:

const sf = require('./node-salesforce-api')

sf.sfConnection.restAction({
  method: 'POST',
  resourceType: 'Account',
  body: {
    Name: 'aName'
  }
}, function (error, response) {
  // ...
})

READ:

const sf = require('./node-salesforce-api')

sf.sfConnection.restAction({
  method: 'GET',
  resourceType: 'Account',
  resourceId: 'abc123'
}, function (error, response) {
  // ...
})

UPDATE:

const sf = require('./node-salesforce-api')

sf.sfConnection.restAction({
  method: 'PATCH',
  resourceType: 'Account',
  resourceId: 'abc123',
  body: {
    Name: 'newName'
  }
}, function (error, response) {
  // ...
})

DESTROY:

const sf = require('./node-salesforce-api')

sf.sfConnection.restAction({
  method: 'DELETE',
  resourceType: 'Account',
  resourceId: 'abc123'
}, function (error, response) {
  // ...
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.