npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

salesforce-api

v0.3.11

Published

Module to invoke salesforce.com services

Downloads

26

Readme

SalesForce client for Nodejs

This node module provides a set of methods to interact against SalesForce's services. The module was created as part of KidoZen project, as a connector for its Enterprise API feature.

Installation

Use npm to install the module:

> npm install salesforce-api

API

Due to the asynchrounous nature of Nodejs, this module uses callbacks in requests. All callbacks have 2 arguments: err and data.

function callback (err, data) {
	// err contains an Error class instance, if any
	// data contains the resulting data
} 

Authentication

UserName and Password login

The module exports a Class and its constructor requires a configuration object with following properties

  • username: Required string. End-user’s username.
  • password: Required string. End-user’s password.
var SalesForce = require("salesforce-api");
var salesforce = new SalesForce();
salesforce.authenticate({
        username: "[email protected]",
        password: "secret"
    }, ...

Username and Password Login (OAuth2 Resource Owner Password Credential)

When OAuth2 client information is given to ctor. authenticate(username, password) uses OAuth2 Resource Owner Password Credential flow to login to Salesforce.

  • username: Required string. End-user’s username.
  • password: Required string. End-user’s password.
  • oauth2: Required. OAuth2 information
var SalesForce = require("salesforce-api");
var salesforce = new SalesForce();
salesforce.authenticate({
        username: "[email protected]",
        password: "secret",
        oauth2 : {
            clientId : '...',
            clientSecret : '...',
            redirectUri : 'https://login.salesforce.com/services/oauth2/token'
        }
    }, ...

Access Token

After the authenticate API call, you can get Salesforce access token and its instance URL. Next time you can use them to establish connection.

var OAUTH2SessionInfo = {
      	instanceUrl : '<your Salesforce server URL (e.g. https://na1.salesforce.com) is here>',
      	accessToken : '<your Salesforce session ID is here>'
   		// you can find this values in the result of authenticate method
    };

var query = {
    credentials : OAUTH2SessionInfo,
    SOSQL: "SELECT Id from Account"
};

api.Query(query, function(err, result) {
	...
	...
	...

Methods

All public methods has the same signature, their have two arguments: options and callback.

  • options must be an object instance containig all parameters for the method.
  • callback must be a function.

Describe(options, callback)

This method should be used to retrieve metadata for an object.

Parameters:

  • options: A required object instance:
    • objectClass: Optional string.
  • callback: A required function for callback.
salesforce.Describe({ objectClass:"Account" }, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

DescribeGlobal(options, callback)

This method returns all SObject information registered in Salesforce

Parameters:

  • callback: A required function for callback.
salesforce.DescribeGlobal(function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

SOSQL Query(options, callback)

Use this method to execute a SOQL query that returns all the results in a single response, or if needed, returns part of the results and an identifier used to retrieve the remaining results.

Parameters:

  • options: A required object instance:
    • SOSQL: Required string.
  • callback: A required function for callback.
salesforce.Query({ SOSQL: "SELECT Id, Name, BillingCity FROM Account" }, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

Json Query(options, callback)

Use this method to execute a SOQL query that returns all the results in a single response, or if needed, returns part of the results and an identifier used to retrieve the remaining results.

Parameters:

  • options: A required object instance:
    • Entity: Required string.
    • Conditions: Required string.
    • Fields: Required string.
    • Options: Required string.
  • callback: A required function for callback.
var query = {
                Entity: "Account",
                Conditions : { 
                    Name : { $like : 'G%' }
                },
                Fields : {
                    Id: 1,
                    Name: 1,
                    CreatedDate: 1 
                },
                Options : { 
                    Limit : 5,
                    Skip: 10
                }
            };
salesforce.Query(query, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

Create(options, callback)

This method allows you to create a new record. You have to supply the required field values of the resource.

Parameters:

  • options: A required object instance:
    • Entity: Required string.
    • Details: Required object instance
  • callback: A required function for callback.

The following example creates a new Account record:

var newAccount = { Name: "Foo account" };
salesforce.Create({ Entity: "Account",  Details: newAccount }, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

Update(options, callback)

Use this method to update records. Provide the updated record information at the data property.

Parameters:

  • options: A required object instance:
    • Entity: Required string.
    • Details: Required object. Updated record. It must have the id property with the object id to update
  • callback: A required function for callback.

In the following example, the Billing City within an Account is update.

var options = {
	Entity: "Account",
	Details : 
		{
			id: "CD656092",
			BillingCity: "San Francisco" 
		}
	}
};

salesforce.Update(options, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

Upsert(options, callback)

Will upsert a record or records given in first argument. External ID field name must be specified in second argument.

Parameters:

  • options: A required object instance:
    • Entity: Required string.
    • Details: Required object. Updated record. It must have the ExtId__c property with the object id to update
    • ExternalIdName the field id name
  • callback: A required function for callback.
var options = {
	Entity: "UpsertTable__c",
	Details : 
		{
			Name : 'Record #1',
  			ExtId__c : 'ID-0000001'
  		},
	ExternalIdName : 'ExtId__c'
	}
};

salesforce.Upsert(options, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

Delete(options, callback)

Use this method to delete an existing record.

Parameters:

  • options: A required object instance:
    • Entity: Required string.
    • Details: Required, string or array of strings with the ids to delete
  • callback: A required function for callback.

In the following example removes the record the ID 1234 from Account

var options = {
	Entity: "Account",
	Details: "1234"
};

salesforce.Delete(options, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});

or

var options = {
	Entity: "Account",
	Details: ["1234","012"]
};

salesforce.Delete(options, function(err, result) {
	if (err) return console.error (err);
	console.log (result);
});