In this post, we gonna make a connection to Salesforce with Node.js and we will update the Salesforce Objects.
We will use node-salesforce
npm module to authenticate with Salesforce. So let’s install it.
Let initialize the project with npm init --yes
and isntall node-salesforce
as shown below.
1 2 3 4 |
$ mkdir salesforceDemo $ cd salesforceDemo $ npm init --yes $ npm install node-salesforce |
Once you install the dependency, let create a file called index.js
, file name can be anything.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
const salesForceLoginUrl = 'https://test.salesforce.com' const salesForceLoginUser = "login user" const salesForceAccountPassword = "account password" const salesForceSecurityToken = "security token" const sf = require('node-salesforce') const connect = async () => { return new Promise((resolve, reject) => { const conn = new sf.Connection({ loginUrl: salesForceLoginUrl }) conn.login(salesForceLoginUser, `${salesForceAccountPassword}${salesForceSecurityToken}`, function (err, result) { if (err) { return reject(err) } return resolve(result) }) }); } (async() => { const salesForceConnct = await connect(); console.log(salesForceConnct) })() |
update the connection settings with your salesforce details. After updating the details you can test your connection with node index.js
and upon the success, you should get a response object with organizationId, id, URL.
Fetech data from salesforce
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
const getObject = async(conn, objectName, where) => { return new Promise((resolve, reject) => { conn.sobject(objectName) .find(where) .execute(function(err, records) { if (err) { return reject(err) } return resolve(records) }); }); } //usage (async() => { try { const salesForceConnct = await connect(); const results = await getObject(salesForceConnct,'ObjectName',{'Id' : '1123455'}); console.log(results); } catch(error) { console.log(error.toString()); } })() |
Update salesforce data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
const updateObject = async (conn, objectName, data) => { return new Promise((resolve, reject) => { conn.sobject(objectName).update(data, function (err, rets) { if (err) { return reject(err); } return resolve(rets); }) }); } //usage (async() => { try { const salesForceConnct = await connect(); const results = await updateObject (salesForceConnct,'ObjectName',{'Id' : '1123455', 'Name' :'Arjun'}); // it will update name where ID = 1123455 console.log(results); } catch(error) { console.log(error.toString()); } })() |
To update multiple rows, just send data in the array of objects format.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.