Salesforce Developers Blog

Recipes for Accessing Heroku Data from Salesforce Functions

Avatar for Julián DuqueJulián Duque
Back in March 2022, we announced a new feature, Heroku Data in Salesforce Functions, which lets you securely access Heroku Postgres, Heroku Data for Redis, and Apache Kafka on Heroku directly from your function.
Recipes for Accessing Heroku Data from Salesforce Functions
November 22, 2022
Listen to this article
0:00 / 0:00

Back in March 2022, we announced a new feature, Heroku Data in Salesforce Functions, which lets you securely access Heroku Postgres, Heroku Data for Redis, and Apache Kafka on Heroku directly from your function. Today, we are releasing a new set of recipes on our Functions Recipes sample application showing you how to write code in Java and Node.js to access Postgres and Redis resources from a function, and how to write unit tests for them.

Screenshot of the Functions Recipes sample application showing the Heroku Postgres recipe

Heroku Postgres

Node.js

To access Heroku Postgres from a Node.js function, we are using the node-postgres npm package. There is no restriction on using a different one, or relying on a query generator or ORM, if that’s what you need.

Two things to keep in mind:

  • Install the dependency on your Functions project by running npm install
  • Access the Heroku Data resource by using the DATABASE_URL environment variable, which can be set manually on the local development experience, or by attaching the resource to the compute environment
1/**
2 * Connects to the PostgreSQL instance.
3 * @param {ClientOptions} input The options to create a PostgreSQL client
4 * @returns {Client} A connected PostgreSQL client
5 */
6export async function pgConnect({ url }) {
7  if (!url) {
8    throw new Error(
9      "database url is not set, please set up the DATABASE_URL environment variable"
10    );
11  }
12
13  // Connect to PostgreSQL
14  const client = new Client({
15    connectionString: url,
16    ssl: {
17      rejectUnauthorized: false
18    }
19  });
20
21  await client.connect();
22
23  return client;
24}

You can find the full source for the Postgres recipe with Node.js in the Functions Recipes GitHub repository.

Java

For Java, we are using the org.postgresql JDBC driver. Similar to the Node.js recipe, we will need to add this dependency to the pom.xml file and rely on the DATABASE_URL to create the connection to the database.

1/**
2   * Get a connection to the database.
3   *
4   * @return Connection
5   * @throws SQLException
6   */
7  protected Connection getConnection() throws SQLException {
8    // If there is already a connection reuse it
9    if (connection != null && !connection.isClosed()) {
10      return connection;
11    }
12
13    try {
14      Class.forName("org.postgresql.Driver");
15      URI dbUri = new URI(this.url);
16
17      // Extract username and password from DATABASE_URL
18      String username = dbUri.getUserInfo().split(":")[0];
19      String password = dbUri.getUserInfo().split(":")[1];
20
21      // Construct a valid JDBC URL
22      String dbUrl =
23          "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
24
25      // Connect to PostgreSQL instance
26      connection = DriverManager.getConnection(dbUrl, username, password);
27
28      return connection;
29    } catch (URISyntaxException | ClassNotFoundException e) {
30      throw new RuntimeException(e);
31    }
32  }

You can find the full source for the Postgres recipe with Java in the Functions Recipes GitHub repository.

Heroku Data for Redis

Node.js

To access Heroku Data for Redis from a Node.js function, we are using the node-redis official npm package, but there is no restriction on using any other library from the npm ecosystem, for example, the popular ioredis client.

Make sure to install the dependency of your choice and connect using the REDIS_URL environment variable provided by Heroku Data.

1/**
2 * Connects to a Redis instance.
3 * @param {ClientOptions} input The options to create a Redis client
4 * @returns {RedisClientType} A connected Redis client
5 */
6export async function redisConnect({ url }) {
7  if (!url) {
8    throw new Error(
9      `database url is not set, please set up the REDIS_URL environment variable`
10    );
11  }
12
13  // Connect to Redis
14  const redisClient = createClient({
15    url,
16    socket: {
17      tls: true,
18      rejectUnauthorized: false
19    }
20  });
21  await redisClient.connect();
22  return redisClient;
23}

You can find the full source for the Redis recipe with Node.js in the Functions Recipes GitHub repository.

Java

For Java, we are using the Jedis client, but you can rely on any other client of your choice (see a list of available clients per programming language). Just make sure to reference the library in your pom.xml file.

1/**
2   * Get a connection to the Redis database.
3   *
4   * @return Jedis
5   */
6  protected Jedis getConnection() {
7    if (connection != null && connection.isConnected()) {
8      return connection;
9    }
10
11    try {
12      TrustManager bogusTrustManager = new X509TrustManager() {
13        public X509Certificate[] getAcceptedIssuers() {
14          return null;
15        }
16
17        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
18
19        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
20      };
21
22      SSLContext sslContext = SSLContext.getInstance("SSL");
23      sslContext.init(null, new TrustManager[] {bogusTrustManager},
24          new java.security.SecureRandom());
25
26      HostnameVerifier bogusHostnameVerifier = (hostname, session) -> true;
27
28      connection = new Jedis(URI.create(this.url), sslContext.getSocketFactory(),
29          sslContext.getDefaultSSLParameters(), bogusHostnameVerifier);
30      return connection;
31    } catch (NoSuchAlgorithmException | KeyManagementException e) {
32      throw new RuntimeException(e);
33    }
34  }

You can find the full source for the Redis recipe with Java in the Functions Recipes GitHub repository.

Bonus: Tests!

We’ve implemented a new feature in the Functions Recipes sample application! Now, you will be able to see the source code of all the test files within the app. Remember, it is a best practice to always write tests for your code. With these recipes, you now have a good way to see how functions can be tested, and external resources like database connections can be mocked, using tools recommended by the Salesforce Functions team.

Screenshot of the Functions Recipes sample app showing the unit tests file for the Heroku Data for Redis recipe

What’s next?

Learn more about Salesforce Functions in our Developer Center with curated content and resources created by our team and other folks at Salesforce.

I also presented a series of videos on how to connect to Heroku Data from Functions, including Apache Kafka. Take a look to learn more:

About the author

Julian Duque

Julián Duque is a Principal Developer Advocate at Salesforce. He is a developer and educator and spends his time running TTRPG games online, as well as playing and training his Mini Aussie, Cumbia.

More Blog Posts

Why the Heroku PostgreSQL Connector for Data Cloud is Important for Developers

Why the Heroku PostgreSQL Connector for Data Cloud is Important for Developers

This blog post explores the new Heroku PostgreSQL Connector, why it is important for developers, and the new possibilities that it opens for Heroku applications.June 25, 2024

The Salesforce Developer’s Guide to the Winter ’26 Release

The Salesforce Developer’s Guide to the Winter ’26 Release

Learn about highlights for developers in the Winter '26 release across Lightning Web Components, Apex, Salesforce Platform developer tools, APIs, and more.September 08, 2025