Technical Article

Node Script to call Salesforce from external system

,

Salesforce runs batch jobs in its server which takes resources and time and creates a burden on the overall server. Hence often times there is a need for a mechanism to call Salesforce system from external system. Calling salesforce from external system results in less burden on the salesforce server and faster processing. By leveraging node js library called JSFORCE this can be achieved. Below is a script which can be run in any external system like AWS, Heroku which will call Salesforce and enable to user to run complex logic from external system.  This script can also be scheduled to run at certain intervals using lambda functions in AWS.

var express = require( 'express' );
var http = require( 'http' );
var jsforce = require('jsforce');

var app = express();
app.set( 'port', process.env.PORT || 3011 );
app.get('/', function (req, res) {
var conn = new jsforce.Connection({
// you can change loginUrl to connect to sandbox or prerelease env.
loginUrl : 'https://test.salesforce.com'
});
var username = 'username sandbox';
var password = 'password';
var apexBody = "System.debug('Hello, World');";//This can be changed to call any salesforce logic from anonymous apex. 
conn.login(username, password, function(err, userInfo) {
if (err) { return console.error(err); }

console.log(conn.accessToken);
console.log(conn.instanceUrl);
const conn2 = ({
instanceUrl :conn.instanceUrl,
accessToken :conn.accessToken 
});
conn.tooling.executeAnonymous(apexBody, function(err, res) {
if (err) { return console.error(err); }
console.log("compiled?: " + res.compiled); // compiled successfully
console.log("executed?: " + res.success); // executed successfully
});
// logged in user property
console.log("User ID: " + userInfo.id);
console.log("Org ID: " + userInfo.organizationId);
// ...
res.send('JSForce Connect Successed!');
});
//res.send('Hello World');
});

http.createServer( app ).listen( app.get( 'port' ), function (){
console.log( 'Express server listening on port ' + app.get( 'port' ));
});

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating