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
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
You can download file directly from github by Click Here.
Create Node Index.js
Step 1:- Create Node Index.js
|
const pg = require('pg')
var bodyParser = require('body-parser')
const pool = NEW pg.Pool({
USER:"asscttojyomvfdxxx",
password:"0298efdb7b22ab5cef10c07da0a4009767eb9934729eea8xxx",
host:"ec2-18-232-254-253.compute-1.amazonaws.com",
port:5432,
DATABASE:"dc0jb3labjb46m",
ssl:TRUE
});
const express = require('express')
const app = express()
const port = 4567
const pug = require('pug');
// Compile the SOURCE code
const compiledFunction = pug.compileFile('template.pug');
app.use(bodyParser.json());
// npm install body-parser (need install this command)
process.env.NODE_TLS_REJECT_UNAUTHORIZED='0'
// START FOR port terminal:- node INDEX.js
// clear the terminal:- ctrl + c
app.get('/data', (req, res) => {
// console.log("Results of rows is",results.rows);
//node localserver.js (run ON terminal TO CREATE NEW LOCAL api url )
//npm install pg (Install pg module FOR run localhost)
pool.connect(FUNCTION(err, client, done) {
IF(err) {
RETURN console.error('connexion error', err);
}
client.query("Select Id, sfid, Name From mysalesforce.scoreCard__c", [], FUNCTION(err, RESULT) {
// CALL `done()` TO release the client back TO the pool
done();
IF(err) {
RETURN console.error('error running query', err);
}
res.send(compiledFunction({
DATA:RESULT.rows
}))
});
});
}); // END GET method
app.post('/update', (req, res) => {
var updateScoreName = req.body.Name
var updateScoreCity = req.body.City__c
var scoreId = req.body.sfid
pool.connect(FUNCTION(err, client, done) {
IF(err) {
RETURN console.error('connexion error', err);
}
client.query("Update mysalesforce.scoreCard__c set Name=$1, City__c=$2 where sfid=$3", [updateScoreName,updateScoreCity,scoreId], FUNCTION(err, RESULT) {
done();
IF(err) {
RETURN console.error('error running query', err);
}
res.send(compiledFunction({
DATA:RESULT.rows
}))
});
});
}); // END UPDATE method
app.post('/insertdata', (req, res) => {
var insertScoreName = req.body.Name
var insertScoreCity = req.body.City__c
pool.connect(FUNCTION(err, client, done) {
IF(err) {
RETURN console.error('connexion error', err);
}
client.query("INSERT INTO mysalesforce.scoreCard__c(Name, City__c)VALUES ($1, $2)", [insertScoreName,insertScoreCity], FUNCTION(err, RESULT) {
done();
IF(err) {
RETURN console.error('error running query', err);
}
res.send(compiledFunction({
DATA:RESULT.rows
}))
});
});
}); // END INSERT method
app.post('/deleteRecord', (req, res) => {
var insertScoreName = req.body.Name
pool.connect(FUNCTION(err, client, done) {
IF(err) {
RETURN console.error('connexion error', err);
}
client.query("DELETE FROM mysalesforce.scoreCard__c WHERE Name = $1", [insertScoreName], FUNCTION(err, RESULT) {
// CALL `done()` TO release the client back TO the pool
done();
IF(err) {
RETURN console.error('error running query', err);
}
res.send(compiledFunction({
DATA:RESULT.rows
}))
});
});
}); // END DELETE method
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Install localserver (npm install localserver )
Step 2:- Create a file localserver.js
|
const ngrok = require('ngrok');
(async function() {
try {
const url = await ngrok.connect(4567);
console.log("-----callBackUrl----",url)
// await ngrok.disconnect();
} catch (error) {
console.log(error)}
})();
Further post that would you like to learn in Salesforce
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
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 |