Send Slack Messages and Notifications via Salesforce Flow

Getting real time notifications when critical events occur in your Salesforce Org can be crucial for your organization's ongoing success. Salesforce offers some built in functionality for messaging, however this can at times be cumbersome for users to manage and cause unwanted delays in the transfer of information. Because many organizations use Slack as their primary messaging channel, it would make sense to send certain critical events from your Salesforce Org directly to team members in Slack.

In this article we will create a simple flow in Salesforce that will send a simple Slack message to a Sales slack channel every time a new lead is created. In order to do so, we will follow the following steps:

  1. Create a New Slack App (15 min)
  2. Install the Halosight "Send Slack Messages" Unmanaged Package (5 min) (This package contains the APEX code needed to make callouts to the Slack App)
  3. Create a flow that invokes the Halosight Send Slack Messages APEX action (15 min) to send a message to Slack. When we are done, your Salesforce Org will be sending Slack notifications every time a lead is created.

Create a New Slack App

In order to give Salesforce the necessary permissions to post messages to our Slack Workspace, we first need to create a Slack App.

  • Go to https://api.slack.com
  • After logging in, navigate to Your Apps in the top right corner.
  • Click Create New App and fill out the form accordingly.
  • Note that the Development Slack Workspace will be the location that the app is installed to.

Now that our app is created, we simply need to give it a few special permissions to allow external sources to post messages.

  • Navigate to OAuth & Permissions on the sidebar.
  • Scroll down to Scopes, this is where we can give the Bot User specific permissions in the Slack App. If you would like to know more about scopes in Slack, you can read their docs here.

Click Add an OAuth Scope and add the following scopes to your app:

  • channels:read
  • chat:write
  • groups:read
  • im:read
  • im:write
  • incoming-webhook
  • mpim-read
  • mpim-write

Great! Now you're ready to install this app to your Workspace. Scroll up and click Install App to Workspace.

No alt text provided for this image

When installing the app, Slack will ask which channel you would like the app to be installed to. This is important because the app will only post messages to channels that it has been invited to. For this demo, we have created a channel called new leads which we will be using.

* If you want to use your Slack App in other channels later on, simply navigate to that channel in Slack and enter /invite @YourAppName in the message bar, then your app will be added.

Wow! That was easy! You will now see that an OAuth Token has been created for you. Copy that token and save it somewhere safe; we will need it for later.

Install the Halosight 'Send Slack Messages' Unmanaged Package

You can install the Send Slack Messages Package into your Salesforce Organization by navigating to the link below:

https://login.salesforce.com/packaging/installPackage.apexp?p0=04t2H00000126Q7QA

It's as simple as that! At the click of a button the app will be installed to your org. If you inspect the contents of the package you will find the following components being installed:

  1. An invocable APEX method that posts a message to Slack,
  2. a second invocable APEX method that facilitates the use of API callouts from Salesforce,
  3. and finally a Named Credential that stores the base URL for the Slack API connection.

Use the Send Slack Message action in a Flow

In this section we will demonstrate the power of this new Unlocked Package with just a few simple clicks.

  • In your Salesforce Org launch the Flow Builder by going to Setup > Flows and click New Flow.
  • In order to perform an action every time a lead record is created, we will take advantage of the Record-Triggered Flow. See the Salesforce Docs to understand more about the power of Record-Triggered Flows.
No alt text provided for this image
No alt text provided for this image
  • Once inside the flow builder, ensure that the record trigger occurs when A record is created and the flow is ran After the record is saved.
  • Now we need to select the object that the flow will listen to. Click Choose Object and search for Lead.

Note that while selecting the object, you can specify the conditions upon which this flow will be run. This may be useful in order to define when a message should be sent. For example, if a salesperson creates the lead, then there is no need to send them a Slack message about it. However, if the lead comes in from an external source, like web to lead, then a Slack message should be sent notifying the salesperson of the external lead. For simplicities sake, we will ignore conditions for now and set Condition Requirements to none.

  • From the Elements side bar, drag Actions onto the Flow and search for 'Send Slack Message'.
No alt text provided for this image

Now all that's left to do is configure the action and create the message we want to send!

  • Let's start out with the Message. For this value, we are going to click on New Resource, and we will create a Text Template resource.
No alt text provided for this image
  • Make sure to set the Record Type to Text Template. And make sure that the body is set to View as Plain Text. The message will not send if it is marked as View as Rich Text.
  • We can now use the Body to create our Slack message text! Note that we can use a simple markup language called mrkdwn to further style the message.
  • Notice that we can also easily add flow variables into the message by clicking Insert Resource. In this case, information about the new lead will be stored under the Record variable.
No alt text provided for this image
  • Copy and Paste the following into the Body:

A New Lead was created for {!$Record.FirstName} {!$Record.LastName}! <{YOUR ORG DOMAIN}/lightning/r/Lead/{!$Record.Id}/view|Click Here to See it Now!>

Make sure to replace {YOUR ORG DOMAIN} with the base URL of your org. For example, mine would look like: https://testhalosight.lighting.force.com/lightning/r/Lead/{!Record.Id}/view

What is that crazy syntax at the end?

As mentioned above, you may use the mrkdwn language to style the message being sent to Slack. In this example we are trying to send a link which references the new Lead record. In order to create a url, the syntax looks like this:

<www.google.com|Click Here to Go to Google!>

Once sent to slack, the above message would look like this:

Click Here to Go to Google!

Back to configuring the Action

The rest of the setup is fairly straightforward. Click here for more information on the following parameters.

  • Remember that OAuth token that you copied after you installed the Slack App to your workspace? Paste that token into the Bearer Token input box.
  • Set Message Preview to "A New Lead was Created".
  • Set Send Request Asynchronously to True. (This option must always be set to true if you are using the action in a Record-Triggered Flow).
  • Set Slack Channel Name to the exact channel name you want to post to. Note: make sure that your Slack app is added to that channel by typing /invite @YourAppName in the message bar of the desired channel.
  • If you would like to mention a specific user, you simply need to paste the user's member ID into the User Mentions field. This ID can be found in Slack by clicking on a user's profile picture > View full profile > More.
No alt text provided for this image

And just like that, you're done! Make sure to save and activate the flow, and now you're ready to test the action by creating a new Lead in your org.

If a message does not get sent to Slack, go to the Slack Channel you are trying to post in and type /invite @YourAppName into the message bar to allow the app to post into the channel. You should be able to see in Slack that the app was added.

No alt text provided for this image

If everything works as expected, you should see a new message in Slack when a Lead is created.

No alt text provided for this image

Amazing! After just a little bit of setup, you now have a powerful APEX Action in your org that can be reused anywhere, and best of all, it's absolutely free! Halosight works hard to give you the most value out of your Salesforce Organization, go to https://www.halosight.com to learn more about us.

Resources

For more in depth information about the Send Slack Message component, view our documentation in GitHub.