Salesforce Subscription to Google Pub/Sub Topic using Google Cloud Functions, Secret Manager, and Python

Emilio Taylor
5 min readJun 13, 2020

Introduction

What if Salesforce could subscribe to events occurring within GCP? What if that subscription could generate data in Salesforce seamlessly? It’s not as hard as you think. Please follow along …

In this lesson, our challenge will be to:

  • Create a Google Pub/Sub Topic
  • Enable Message Published to Topic to Trigger Google Cloud Function
  • Store the Salesforce credentials in GCP using Secret Manager
  • Use the Cloud Function to authenticate with Salesforce, and Create Salesforce Service Cloud Case

Note (10/22/2020): Curious about this same pattern on AWS? I’ve got you covered. Click Here!

Prerequisites

Architecture

Step 1: Google Cloud Platform: Enable Secret Manager

Accessing Salesforce through the REST API requires three (3) parts; Username, Password, and Security Token. The most secure option for storing this type of information within GCP is through Secret Manager. This option allows the storing and version control for these credentials for secure usage. Here’s how to set this up.

Steps:

  • From the Console, select the Google Cloud Platform menu
  • On the menu, select Security
  • On Security select Secret Manager

Step 2: Google Cloud Platform: Create Secret for Salesforce Credentials

Steps:

  • On the Secret Manager, select Create Secret
  • On the Create Secret page, provide the following
  • Name = SF_USER_PROD
  • Secret Value = salesforce@username.com
  • Complete using the Create Secret button
  • Repeat these same steps for SF_PASS_PROD and SF_TOKEN_PROD

Step 3: Google Cloud Platform: Create Pub/Sub Topic

Using Pub/Sub in an enterprise data flow design introduces a myriad of integration options and distributed capabilities. In this example, we’ll simply create a Topic for Case messages to be stored and subscribed too using Salesforce.

Steps:

  • On the menu, Select Pub/Sub > Topics
  • On the Topics page, select Create Topic
  • On the Create Topic page, enter the name of the topic, provide the following
  • Name = pub-salesforce-cases
  • When done, select Create Topic
  • Once the topic has been created, select Trigger Cloud Function

Step 4: Google Cloud Platform: Trigger Cloud Function from Pub/Sub Topic (Python)

Now that credentials have been safely stored, and the Pub/Sub topic has been created, we want to make sure that any message published to this topic will trigger the Cloud Function. This Cloud Function will need to Parse the Pub/Sub message, Authenticate to Salesforce and Send a New Case to Salesforce using a third-party REST API client for Python called Simple Salesforce. This allows us to authenticate, create, update, and query Salesforce data through the REST API using simplified methods.

Steps:

  • From the pub-salesforce-cases Topic, select Trigger Cloud Function
  • On the Create Function page, provide the following
  • Name = pubsub-salesforce-cases
  • Trigger =Cloud Pub/Sub
  • Select a Cloud Pub/Sub topic = projects/[project name]/topics/pub-salesforce-cases
  • Memory Allocated = 128 MiB
  • Source Code > Inline editor (selected)
  • Source Code > Runtime = Python 3.7
  • Place the following in Main.py editor. This is the code where we capture the Pub/Sub message, authenticate to Salesforce and take the message to submit to Salesforce as a new Case
  • Place the following in Requirements.txt editor. This adds dependencies for Python to reference Requests, Simple Salesforce, and Google Secret Manager
  • Function to execute = main
  • Select Environment Variables, Networking, Timeouts and More
  • Environment > Environment Variables select Add Variable
  • Add Name = sftype_object, Value = Case
  • Add Name = gcp_project_id, Value = Your Project ID for GCP. This is stored on Secret Manager > Secret > Secret Details
  • Select Create to Complete the Function, and proceed with Deployment

Step 5: Testing Using Salesforce (Case)

Now that this Cloud Function has been created, we can now proceed to publish a new Pub/Sub message that translates into a Salesforce case.

Steps:

  • On the pubsub-salesforce-cases Cloud Function
  • Select the Trigger tab
  • On the Trigger tab, select the topic
  • On the topic page, select Publish Message
  • On the Publish message page, in the Message body enter the following JSON
{
"Subject":"Google Cloud Works!",
"Description":"This Works Perfectly!",
"Status":"New"
}
  • Once entered, select Publish
  • Login to Salesforce, search Cases for New Entry and Review

What’s Next?

Having Google Pub/Sub as part of your distributed messaging design is essential to scalability and reducing dependencies between disparate systems. Challenge yourself to create multiple subscriptions to the same topic or trigger additional Cloud Functions to take full advantage of an ESB (Enterprise Service Bus) architecture. Remember, if you’re interested in how to do this in AWS, proceed to my AWS/Salesforce article for more details.

--

--

Emilio Taylor

Salesforce Practice Leader, Technology Enthusiast, Entrepreneur, Integrator, Architect, Developer, and Overall Cloud Advocate.