Tony Ruth
Zerion Engineering
Published in
8 min readJun 19, 2018

--

Stream Financial Data to Slack from Salesforce (Code Included)

This post will walk through the steps to send a live stream of financial data to Slack every time a new sale comes through.

Celebrating new wins, and tracking the progress of your financial goals are key to every business. How do you ensure that everyone knows when a new deal is closed even if they aren’t selling? Easy, send the summary data to a dedicated Slack channel and watch the emoji responses pile on.

  • *Salesforce Enterprise Edition is required to implement this workflow (requires the use of Apex code).**
  • *Please review this video if you want to include links to the related files of your opportunity (Opportunity Items). It is how I included links to the files in the Slack message. *

The general approach is to use Salesforce as the order processing and workflow platform, and Slack as the notification platform. In the middle is a subscription management platform SaasOptics which integrates nicely with Salesforce and stores the financial data in a custom object. This allows us to leverage the workflow and development capabilities within Salesforce to make the magic happen.

  • If you are not using SaaSOptics, this approach will still work for you using any custom object that contains an amount and a date.

The format of this article will be separated into six key parts, each with their own set of steps. By the end, you will be able to visualize your sales data in Slack, just like the example above.

Part 1) Setup slack channel and integration

a) Create a new channel in your slack organization.

b) Setup a new integration for this channel under Apps=>Custom Integrations=>Incoming WebHooks.

You can copy and paste the link below and replace {companyName} with your company name to gain quick access to the integrations page.

https://{companyName}.slack.com/apps/manage/custom-integrations

c) Complete the integration setup, copy the WebHook URL, and place it somewhere safe.

d) Create one more WebHook which sends a message directly to you (for testing purposes) and store that somewhere safe as well.

Part 2) Create a Salesforce Sandbox

Salesforce doesn’t allow you to write Apex code on the live instance of your account, so you have to create a sandbox account.

PLEASE NOTE: If this is your first time hearing the word or working with Apex in Salesforce, don’t worry, no major coding is required.

To create a sandbox follow the steps in the screenshots below.

a) Select the setup option from the gear icon

b) Search for sandbox within the setup menu and select Sandboxes

c) Click the create new sandbox button.

PLEASE NOTE: If possible, make sure the backup includes your custom invoice object data so you can test multiple scenarios. If this option is not available, you will need to create some sample data in the sandbox.

d) Define a name for the sandbox and the source to use

e) Go grab some coffee while the sandbox is being created and then move onto part three.

Part 3) Setup the Apex code and matching test

a) Now that the sandbox is setup, login and go to the setup page within the sandbox.

b) In the search bar, type in the word Apex, select Apex Classes, and create a new class. Repeat this step for the test as well (code in the links below).

c) Create a new apex class

d) Open each of the links below and then save each as a new apex class within your sandbox environment.

Code (the actual code that will send messages to the Slack WebHook)

Test (the test case which is required to run the code in the production Salesforce account)

Apex Developer Guide (helpful guide for getting started with Apex)

When you are done, you will have two apex files showing the size and version of the API they require.

e) Copy your WebHook URL from Part 1b, and place the URL for the dedicated channel on line 5 in the code. Do the same for the URL from 1d and add that on line 8.

Part 4) Create a process flow to execute the apex code

Now that we have the code setup to send the data to Slack, we need a way to execute it when a new sale happens. Creating a process flow is a simple way to execute our apex code when a new deal is closed.

a) Search for process builder within the setup menu to create a new process flow by clicking on Process Builder.

b) Create a new process flow from the setup menu

c) Define the details for this process flow and make sure the process starts on a record change.

d) When a new opportunity is marked won, the process will execute the defined action.

The screen shot above shows that when a new opportunity is marked won, then we execute the Send to Slack action (which is our Apex code). The opportunity contains key details for our message so we pass in some of those values using the field mapping shown below.

e) Set the apex variables from the opportunity fields defined below.

Each value is a field reference except for amount which ends up being a formula because the field type in the object is defined as currency. Make sure to activate the process and then close an opportunity in the sandbox environment to test it out.

PLEASE NOTE: If you decide to tune the code then your field mapping may contain more or less values shown above.

f) Activate the process flow, then close an opportunity in the sandbox to test it out.

Part 5) Validate tests and export the resources

Next, when you are satisfied with the results of the process, you’ll need to validate the tests from the test class using the developer console.

a) Comment out the private WebHook URL, and uncomment the dedicated channel URL.

b) Open the developer console under the settings menu.

c) Select a new run under the test menu.

d) Select the test class that was created in part 4d and click Run.

PLEASE NOTE: It is critical the test case covers 70% of the code as that is the required minimum for Salesforce. If you fail to do this, you will end up wasting time exporting the code that will fail deployment validation in production.

e) Review the test coverage for the Apex code from part 3d.

As shown in the screen shot above, the code and test case comes with 96% coverage which is a good foundation to start with. If the coverage is more than 70%, then create a new OutBound Change Set following the steps below.

f) Search for outbound change sets in the setup of the sandbox.

A change set is used to move changes from our sandbox to our production environment. Make sure to include the apex code, test class, and the process flow in the change set. You will find the process flow in the Flow Definition section of the change set menu.

g) Add components to the change set, including the process flow , apex code, and the test.

h) Upload the change set to your production account, grab a coffee, stretch your legs, and come back in ten minutes.

Part 6) Validate the change set on production and deploy

a) On your production Salesforce account, go to the Inbound Change Set section within setup and select the change set that was uploaded in part 5.

b) Validate the change before proceeding with deployment. If you double checked your code coverage before exporting, there should be no surprises when you run the validation.

c) Confirm that the test execution is successful and get ready to deploy the changes.

d) After the validation is complete, deploy the changes to production by clicking on the Quick Deploy option.

e) Double check to make sure the process flow is activated in the production environment and wait for the new deals to start flowing in.

If you have any questions please feel free to drop a comment below. I would be happy to try and help with any implementation challenges you might run into.

--

--