Create Node js Rest Api to get, insert, update and delete records from SOQL Using Express JS, Install pool database, Install pg module and Install localserver for Start port terminal and project structure setup | How to insert, update, retrieve and Delete Record from Node js Rest Api Using Express JS, pool, pg module and localserver in Salesforce (SOQL)

1,304 views


Hey guys, today in this post we are going to learn about How to Create Node js Rest Api to get, insert, update and delete records from SOQL Using Express JS, Install pool database, Install pg and Install localserver to Start port terminal and project structure setup

Real time scenarios:-Install pg module for run localhost, install pool for connect database and install localserver to run local API URL(endpoint url) and Start for port terminal:- node index.js

In this example we are connecting Heroku database (postgres) with authentication of user, password, host, port and database

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

After connecting the database we are insert, update, retrieve and Delete recorda from Salesforce (SOQL) Credentials.

I am using following dependency libraries in this nodejs project:-

  • Express js : Express is a framework for building web applications on top of Node.js
  • Pool database connect:- Use to create connection with Heroku database (postgres) with authentication that allow connect into data table (SOQL).
  • body-parser:- This nodejs module help to reading data from the ‘form’ element and attached with request.
  • template.pug:- Install template pug for display API data in HTML table format.

The Node js Rest API details are as follows:-

Route Method Description
/data Get Get all scoreCard__c (Salesforce Custom Object) data from Salesforce
/update Post Update scoreCard__c (Salesforce Custom Object) record into database Salesforce
/insertdata Post Insert new coreCard__c (Salesforce Custom Object) record into database Salesforce
/deleteRecord Post Delete particular coreCard__c (Salesforce Custom Object) record from database Salesforce

Live Demo

creating a rest api using node.js express and pg module -- w3web.net

 

You can download file directly from github by Click Here.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

 

Create Node Index.js

Step 1:- Create Node Index.js

  1.   const pg = require('pg')
  2. var bodyParser = require('body-parser')
  3.  
  4.  
  5. const pool = NEW pg.Pool({
  6.   USER:"asscttojyomvfdxxx",
  7.   password:"0298efdb7b22ab5cef10c07da0a4009767eb9934729eea8xxx",
  8.   host:"ec2-18-232-254-253.compute-1.amazonaws.com",
  9.   port:5432,
  10.   DATABASE:"dc0jb3labjb46m",  
  11.   ssl:TRUE
  12.  
  13. });
  14. const express = require('express')
  15. const app = express()
  16. const port = 4567
  17. const pug = require('pug');
  18.  
  19. // Compile the SOURCE code
  20. const compiledFunction = pug.compileFile('template.pug');
  21.  
  22.  
  23. app.use(bodyParser.json());                        
  24.  
  25. // npm install body-parser (need install this command)
  26.  
  27. process.env.NODE_TLS_REJECT_UNAUTHORIZED='0'
  28.  
  29. // START FOR port terminal:- node INDEX.js
  30. // clear the terminal:- ctrl + c
  31.  
  32. app.get('/data', (req, res) => {
  33.  
  34.  
  35. //  console.log("Results of rows is",results.rows);  
  36.  
  37. //node localserver.js (run ON terminal TO CREATE NEW LOCAL api url )
  38.  
  39. //npm install pg (Install pg module FOR run localhost)
  40.  
  41. pool.connect(FUNCTION(err, client, done) {
  42.  
  43.   IF(err) {
  44.     RETURN console.error('connexion error', err);
  45.   }
  46.   client.query("Select Id, sfid, Name From mysalesforce.scoreCard__c", [], FUNCTION(err, RESULT) {
  47.     // CALL `done()` TO release the client back TO the pool
  48.     done();
  49.  
  50.  
  51.     IF(err) {
  52.       RETURN console.error('error running query', err);
  53.     }
  54.  
  55.  
  56.     res.send(compiledFunction({
  57.       DATA:RESULT.rows
  58.     }))
  59.  
  60.  
  61.   });
  62. });
  63.  
  64.  
  65. });  // END GET method
  66.  
  67.  
  68. app.post('/update', (req, res) => {
  69.  
  70.   var updateScoreName = req.body.Name  
  71.   var updateScoreCity = req.body.City__c
  72.   var scoreId = req.body.sfid
  73.  
  74.   pool.connect(FUNCTION(err, client, done) {
  75.     IF(err) {
  76.       RETURN console.error('connexion error', err);
  77.     }
  78.     client.query("Update mysalesforce.scoreCard__c set Name=$1, City__c=$2 where sfid=$3", [updateScoreName,updateScoreCity,scoreId], FUNCTION(err, RESULT) {
  79.  
  80.       done();
  81.  
  82.  
  83.       IF(err) {
  84.         RETURN console.error('error running query', err);
  85.       }
  86.  
  87.  
  88.       res.send(compiledFunction({
  89.         DATA:RESULT.rows                
  90.       }))
  91.  
  92.  
  93.     });
  94.   });
  95.  
  96.  
  97.   }); // END UPDATE method
  98.  
  99.  
  100.  
  101.   app.post('/insertdata', (req, res) => {
  102.  
  103.     var insertScoreName = req.body.Name  
  104.     var insertScoreCity = req.body.City__c
  105.  
  106.     pool.connect(FUNCTION(err, client, done) {
  107.       IF(err) {
  108.         RETURN console.error('connexion error', err);
  109.       }
  110.       client.query("INSERT INTO mysalesforce.scoreCard__c(Name, City__c)VALUES ($1, $2)", [insertScoreName,insertScoreCity], FUNCTION(err, RESULT) {
  111.  
  112.         done();
  113.  
  114.  
  115.         IF(err) {
  116.           RETURN console.error('error running query', err);
  117.         }
  118.  
  119.  
  120.         res.send(compiledFunction({
  121.           DATA:RESULT.rows
  122.         }))
  123.  
  124.  
  125.       });
  126.     });
  127.  
  128.  
  129.     }); // END INSERT method
  130.  
  131.  
  132.  
  133.   app.post('/deleteRecord', (req, res) => {
  134.  
  135.      var insertScoreName = req.body.Name      
  136.  
  137.     pool.connect(FUNCTION(err, client, done) {
  138.       IF(err) {
  139.         RETURN console.error('connexion error', err);
  140.       }
  141.       client.query("DELETE FROM mysalesforce.scoreCard__c WHERE Name = $1", [insertScoreName], FUNCTION(err, RESULT) {
  142.         // CALL `done()` TO release the client back TO the pool
  143.         done();
  144.  
  145.  
  146.         IF(err) {
  147.           RETURN console.error('error running query', err);
  148.         }
  149.  
  150.  
  151.         res.send(compiledFunction({
  152.           DATA:RESULT.rows
  153.         }))
  154.  
  155.  
  156.       });
  157.     });
  158.  
  159.  
  160.     }); // END DELETE method
  161.  
  162.  
  163. app.listen(port, () => {
  164.   console.log(`Example app listening at http://localhost:${port}`)
  165. })

Install localserver (npm install localserver )

Step 2:- Create a file localserver.js

  1.    const ngrok = require('ngrok');
  2. (async function() {
  3.   try {
  4.   const url = await ngrok.connect(4567);
  5.   console.log("-----callBackUrl----",url)
  6.   // await ngrok.disconnect(); 
  7. } catch (error) {
  8.     console.log(error)}
  9. })();

creating a rest api using node.js -- w3web.net

 

 

Further post that would you like to learn in Salesforce

 

 

 


 

FAQ (Frequently Asked Questions)

What is Node JS REST API?

REST stands for Representational State Transfer. REST is web standards based architecture and uses HTTP Protocol.

How do I create a secure REST API in node JS?

To make your APIs Restful, you must follow a set of constraints while writing them.

What is REST API services?

A REST API is an application programming interface that conforms to the constraints of REST architectural style and allows for interaction with Restful web services.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

đź“Ś Salesforce Administrators

đź“Ś Salesforce Lightning Flow Builder

đź“Ś Salesforce Record Trigger Flow Builder

👉 Get Free Course →

đź“Ś Aura Lightning Framework

đź“Ś Lightning Web Component (LWC)

đź“Ś Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment