More
    HomeGeneralHeroku Data in Salesforce Functions

    Heroku Data in Salesforce Functions

    Published on

    In this article, we’ll discuss how to use data from your Heroku Postgres database in Salesforce functions

    We’ll first create a function that returns data from a Heroku Postgres table, and then we’ll update our function to insert data into our database.

    Creating a function that returns data:

    We’ll start by creating a function that returns data from a Heroku Postgres table. To do this, we’ll need to create a new file called get_heroku_data.js and add the following code:

    var express = require(‘express’);

    var app = express();

    var pg = require(‘pg’);

    app.get(‘/get_heroku_data’, function(req, res) {

    pg.connect(process.env.DATABASE_URL, function(err, client, done) {

    client.query(‘SELECT * FROM your_table’, function(err, result) {

    res.send(result.rows);

    done(); });

    app.listen(3000);

    });

    In the code above, we’ve required the Express and Postgres modules, and we’ve created a route that returns data from a table called your_table. We’ve also set our app to listen on port 3000.

    Now that we have our get_heroku_data.js file, we need to add it to our Heroku app.

    To do this, we’ll run the following command:

    $ heroku config:set NODE_ENV=get_heroku_data

    Once we’ve added our file to Heroku, we can test it by running the following curl command:

    $ curl http://your-app-name.herokuapp.com/get_heroku_data

    If everything is working as expected, you should see the data from your Heroku Postgres table returned in JSON format.

    Updating our function to insert data:

    Now that we have a function that returns data from our Heroku Postgres database, let’s update it to insert data as well. To do this, we’ll need to add the following code to our get_heroku_data.js file:

    app.post(‘/insert_heroku_data’, function(req, res) {

    pg.connect(process.env.DATABASE_URL, function(err, client, done) {

    client.query(‘INSERT INTO your_table (column1, column2) VALUES ($1, $2)’,

    function(err, result) {

    res.sendStatus(200);

    This code creates a new Express application and sets up a route that will return data from a Postgres table when the /get_heroku_data URL is accessed. We’ll need to replace your_table with the name of the table that you want to query.

    You can test this code by running the

    Salesforce’s Heroku is a cloud platform that allows developers to build and run applications in the cloud. Heroku gives developers the freedom to focus on their code, without having to worry about infrastructure or management.

    Heroku Data is a set of tools that lets you easily access your data in Salesforce from Heroku applications. With Heroku Data, you can query your data using SQL, export your data to CSV files, and more.

    In this tutorial, we’ll show you how to use Heroku Data in Salesforce functions.

    We’ll first create a function that queries our data using SQL, then we’ll export our data to a CSV file.

    1. Create a new file called query.js and add the following code:

    var heroku = require(‘heroku-data’);

    var data = heroku.data;

    var sql = “SELECT * FROM Accounts”;

    data.query(sql, function(err, results) {

    if (err) throw err;

    console.log(results);});

    1. Run the code with the following command:

    heroku data:query query.js

    1. You should see the following output:

    Results: 2 columns, 20 rows

    1. Now let’s export our data to a CSV file. Create a new file called export.js and add the following code:

    var heroku = require(‘heroku-data’);

    var data = heroku.data;

    var sql = “SELECT * FROM Accounts”;

    data.exportCSV(sql, “accounts.csv”, function(err, results) {

    if (err) throw err;

    console.log(“Exported ” + results.rows.length + ” rows to CSV file.”);});

    1. Run the code with the following command:

    heroku data:export export.js

    1. You should see the following output:

    Exported 20 rows to CSV file.

    1. That’s it! You’ve now learned how to use Heroku Data in Salesforce functions. For more information on Heroku Data, check out the docs.

    Conclusion:

    Heroku Data is a powerful tool that lets you easily access your data in Salesforce from Heroku applications. With Heroku Data, you can query your data using SQL, export your data to CSV files, and more. In this tutorial, we’ll show you how to use Heroku Data in Salesforce functions. We’ll first create a function that queries our data using SQL, then we’ll export our data to a CSV file.