title: How to get records from Salesforce and display them on a Heroku app using Node.js
body: <p>I have created an Heroku app to be able to retrieve records (<strong>Contracts</strong>) from Salesforce and display them on my Heroku app. But I am struggling to write the correct request on my node.js code. Basically when pressing &quot;<strong>Retrieve Contracts</strong>&quot;, I would like to get a list of contracts as shown on the pic.</p>
  <p><a href="https://i.stack.imgur.com/48XpW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/48XpW.png" alt="enter image description here" /></a></p>
  <p>But in my case, I get an Error anytime I press &quot;Retrieve Contracts&quot; <strong>{&quot;error&quot;:&quot;relation &quot;contract&quot; does not exist&quot;}</strong></p>
  <p><a href="https://i.stack.imgur.com/7wlF0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7wlF0.png" alt="enter image description here" /></a></p>
  <p>To get the records from Salesforce I'm using &quot;<strong>Express.js</strong>&quot; and &quot;<strong>app.get</strong>&quot; (here is a snippet)</p>
  <pre><code>    app.get('/getContracts', function(req, res) {
  
      pg.connect(process.env.DATABASE_URL, function (err, conn, done) {
  
          // watch for any connect issues
          if (err) {
              console.log(err);
              return;
          }
  
          conn.query('SELECT Name,Product__c FROM Contract WHERE Product__c != null',
          function(err, result) {
              console.log(result)
              if (err) {
                  res.status(400).json({error: err.message});
              }
              else {
                  // Need to display 'Success!'
                  res.json(result);
              }
          });
          
      });
  });
  </code></pre>
  <p>here is the full code:</p>
  <p>server.js:</p>
  <pre><code>var express = require('express');
  var bodyParser = require('body-parser');
  var pg = require('pg');
  
  var app = express();
  
  app.set('port', process.env.PORT || 3000);
  
  app.use(express.static('public'));
  app.use(bodyParser.json());
  
  app.post('/update', function(req, res) {
      pg.connect(process.env.DATABASE_URL, function (err, conn, done) {
          // watch for any connect issues
          if (err) console.log(err);
          conn.query(
              'UPDATE salesforce.Contact SET Phone = $1, HomePhone = $1, MobilePhone = $1 WHERE LOWER(FirstName) = LOWER($2) AND LOWER(LastName) = LOWER($3) AND LOWER(Email) = LOWER($4)',
              [req.body.phone.trim(), req.body.firstName.trim(), req.body.lastName.trim(), req.body.email.trim()],
              function(err, result) {
                  if (err != null || result.rowCount == 0) {
                    conn.query('INSERT INTO salesforce.Contact (Phone, MobilePhone, FirstName, LastName, Email) VALUES ($1, $2, $3, $4, $5)',
                    [req.body.phone.trim(), req.body.phone.trim(), req.body.firstName.trim(), req.body.lastName.trim(), req.body.email.trim()],
                    function(err, result) {
                      done();
                      if (err) {
                          res.status(400).json({error: err.message});
                      }
                      else {
                          // this will still cause jquery to display 'Record updated!'
                          // eventhough it was inserted
                          res.json(result);
                      }
                    });
                  }
                  else {
                      done();
                      res.json(result);
                  }
              }
          );
      });
  });
  
  app.get('/getContracts', function(req, res) {
  
      pg.connect(process.env.DATABASE_URL, function (err, conn, done) {
  
          // watch for any connect issues
          if (err) {
              console.log(err);
              return;
          }
  
          conn.query('SELECT Name,Product__c FROM Contract WHERE Product__c != null',
          function(err, result) {
              console.log(result)
              if (err) {
                  res.status(400).json({error: err.message});
              }
              else {
                  // Need to display 'Success!'
                  res.json(result);
              }
          });
          
      });
  });
  
  app.listen(app.get('port'), function () {
      console.log('Express server listening on port ' + app.get('port'));
  });
  </code></pre>
  <p>index html:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
      &lt;title&gt;CRM AXG&lt;/title&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css&quot;&gt;
      &lt;script src=&quot;//code.jquery.com/jquery-1.11.3.min.js&quot;&gt;&lt;/script&gt;
      &lt;style&gt;
          body {
              padding-top: 60px;
          }
      &lt;/style&gt;
      &lt;script&gt;
          $(function() {
              $(&quot;#phoneChangerForm&quot;).submit(function(event) {
                  event.preventDefault();
  
                  var errorMessage = $(&quot;#errorMessage&quot;);
                  var error = $(&quot;#error&quot;);
                  error.hide();
  
                  $(&quot;#message&quot;).hide();
  
                  var firstName = $(&quot;#firstName&quot;).val();
                  var lastName = $(&quot;#lastName&quot;).val();
                  var email = $(&quot;#email&quot;).val();
                  var phone = $(&quot;#phone&quot;).val();
  
                  if (firstName.length == 0 || lastName.length == 0 || email.length == 0 || phone.length == 0) {
                      errorMessage.text(&quot;All of the fields are required.&quot;);
                      error.show();
                  }
                  else {
                      $.ajax({
                          url: event.target.action,
                          method: event.target.method,
                          data: JSON.stringify({
                              firstName: firstName,
                              lastName: lastName,
                              email: email,
                              phone: phone
                          }),
                          contentType: &quot;application/json; charset=utf-8&quot;,
                          dataType: &quot;json&quot;,
                          success: function(data) {
                              $(&quot;#firstName&quot;).val(&quot;&quot;);
                              $(&quot;#lastName&quot;).val(&quot;&quot;);
                              $(&quot;#email&quot;).val(&quot;&quot;);
                              $(&quot;#phone&quot;).val(&quot;&quot;);
                              $(&quot;#messageMessage&quot;).text(&quot;Record updated!&quot;);
                              $(&quot;#message&quot;).show();
                          },
                          error: function(err) {
                              errorMessage.text(err.responseJSON.error);
                              error.show();
                          }
                      })
                  }
              });
          });
  
      &lt;/script&gt;
  
      &lt;script&gt;
          $(function() {
              $(&quot;#getContractForm&quot;).submit(function(event) {
                  event.preventDefault();
  
                  var errorMessage = $(&quot;#errorMessage&quot;);
                  var error = $(&quot;#error&quot;);
                  error.hide();
  
                  $(&quot;#message&quot;).hide();
  
  
                  $.ajax({
                      url: event.target.action,
                      method: event.target.method,
                      contentType: &quot;application/json; charset=utf-8&quot;,
                      dataType: &quot;json&quot;,
                      success: function(data) {
                          console.log(data);
                          $(&quot;#name&quot;).val(&quot;&quot;);
                          $(&quot;#product__c&quot;).val(&quot;&quot;);
                          $(&quot;#messageMessage&quot;).text(&quot;Success!&quot;);
                          $(&quot;#message&quot;).show();
                      },
                      error: function(err) {
                          errorMessage.text(err.responseJSON.error);
                          error.show();
                      }
                  })
                  
              });
          });
  
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;nav class=&quot;navbar navbar-inverse navbar-fixed-top&quot;&gt;
          &lt;div class=&quot;container&quot;&gt;
              &lt;div class=&quot;navbar-header&quot;&gt;
                  &lt;a class=&quot;navbar-brand&quot; href=&quot;/&quot;&gt;Contact Information&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/nav&gt;
  
      &lt;div class=&quot;container&quot;&gt;
          &lt;form id=&quot;phoneChangerForm&quot; action=&quot;/update&quot; method=&quot;post&quot; style=&quot;width: 400px&quot;&gt;
              &lt;div class=&quot;panel panel-default&quot;&gt;
                  &lt;div class=&quot;panel-heading&quot;&gt;
                      &lt;h3 class=&quot;panel-title&quot;&gt;Contact Information&lt;/h3&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;panel-body&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label for=&quot;firstName&quot;&gt;First Name&lt;/label&gt;
                          &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;firstName&quot; placeholder=&quot;For verification&quot; required&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label for=&quot;lastName&quot;&gt;Last Name&lt;/label&gt;
                          &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastName&quot; placeholder=&quot;For verification&quot; required&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
                          &lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;email&quot; placeholder=&quot;For verification&quot; required&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label for=&quot;phone&quot;&gt;Phone&lt;/label&gt;
                          &lt;input type=&quot;tel&quot; class=&quot;form-control&quot; id=&quot;phone&quot; placeholder=&quot;New Phone Number&quot; required&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;panel-footer&quot;&gt;
                      &lt;div id=&quot;message&quot; class=&quot;alert alert-info&quot; role=&quot;alert&quot; style=&quot;display: none;&quot;&gt;
                          &lt;span class=&quot;glyphicon glyphicon-ok&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
                          &lt;span id=&quot;messageMessage&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;div id=&quot;error&quot; class=&quot;alert alert-danger&quot; role=&quot;alert&quot; style=&quot;display: none;&quot;&gt;
                          &lt;span class=&quot;glyphicon glyphicon-exclamation-sign&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
                          &lt;span class=&quot;sr-only&quot;&gt;Error:&lt;/span&gt;
                          &lt;span id=&quot;errorMessage&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Update Contact&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
      &lt;nav class=&quot;navbar navbar-inverse navbar-fixed-top&quot;&gt;
          &lt;div class=&quot;container&quot;&gt;
              &lt;div class=&quot;navbar-header&quot;&gt;
                  &lt;a class=&quot;navbar-brand&quot; href=&quot;/&quot;&gt;Contract Information&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/nav&gt;
  
      &lt;div class=&quot;container&quot;&gt;
          &lt;form id=&quot;getContractForm&quot; action=&quot;/getContracts&quot; method=&quot;get&quot; style=&quot;width: 400px&quot;&gt;
              &lt;div class=&quot;panel panel-default&quot;&gt;
                  &lt;div class=&quot;panel-heading&quot;&gt;
                      &lt;h3 class=&quot;panel-title&quot;&gt;Contract List&lt;/h3&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;panel-body&quot;&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;panel-footer&quot;&gt;
                      &lt;div id=&quot;message&quot; class=&quot;alert alert-info&quot; role=&quot;alert&quot; style=&quot;display: none;&quot;&gt;
                          &lt;span class=&quot;glyphicon glyphicon-ok&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
                          &lt;span id=&quot;messageMessage&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
  
                      &lt;!-- display retrieve contracts result--&gt;
                      &lt;Table&gt;
                          &lt;tbody&gt;
                              &lt;!-- &lt;jS&gt;data.foreach(d) =&gt; &lt;tr&gt;d.name&lt;/tr&gt;&lt;/jS&gt; --&gt;
                          &lt;/tbody&gt;
                      &lt;/Table&gt;
  
                      &lt;div id=&quot;error&quot; class=&quot;alert alert-danger&quot; role=&quot;alert&quot; style=&quot;display: none;&quot;&gt;
                          &lt;span class=&quot;glyphicon glyphicon-exclamation-sign&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
                          &lt;span class=&quot;sr-only&quot;&gt;Error:&lt;/span&gt;
                          &lt;span id=&quot;errorMessage&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Retrieve Contracts&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
      &lt;nav class=&quot;navbar navbar-inverse navbar-fixed-top&quot;&gt;
          &lt;div class=&quot;container&quot;&gt;
              &lt;div class=&quot;navbar-header&quot;&gt;
                  &lt;a class=&quot;navbar-brand&quot; href=&quot;/&quot;&gt;CRM AXG&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/nav&gt;
  
      &lt;div class=&quot;container&quot;&gt;
          &lt;form id=&quot;getContractForm&quot; action=&quot;/&quot; method=&quot;get&quot; style=&quot;width: 400px&quot;&gt;
              &lt;div class=&quot;panel panel-default&quot;&gt;
                  &lt;div class=&quot;panel-heading&quot;&gt;
                      &lt;h3 class=&quot;panel-title&quot;&gt;Product List&lt;/h3&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;panel-body&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label for=&quot;contractName&quot;&gt;Product Name&lt;/label&gt;
                          &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;contractName&quot; placeholder=&quot;For verification&quot; required&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;panel-footer&quot;&gt;
                      &lt;div id=&quot;message&quot; class=&quot;alert alert-info&quot; role=&quot;alert&quot; style=&quot;display: none;&quot;&gt;
                          &lt;span class=&quot;glyphicon glyphicon-ok&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
                          &lt;span id=&quot;messageMessage&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;div id=&quot;error&quot; class=&quot;alert alert-danger&quot; role=&quot;alert&quot; style=&quot;display: none;&quot;&gt;
                          &lt;span class=&quot;glyphicon glyphicon-exclamation-sign&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
                          &lt;span class=&quot;sr-only&quot;&gt;Error:&lt;/span&gt;
                          &lt;span id=&quot;errorMessage&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Retrieve Products&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>