Post a Message to a Slack Channel using Flow

Post a Message to a Slack Channel using Flow

Last Updated on May 29, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

  • How do you post a message to a Slack channel via Salesforce Flow? 
Slack is a messaging app for businesses that connects people to the information they need. Its workspaces allow you to organize communications by channels for group discussions and allow for private messages to share information, files, and more all in one place. Plus, Slack integrates with a host of other apps so you can manage your entire workflow through one platform.

Objectives:

After reading this blog, you’ll be able to: 

  • Understand what a Slack Channel is 
  • Understand Message Destination and Slack Auth objects
  • Use get records to avoid hard coding of Id
  • @Mention Slack user in a post
  • Send a message to the Slack channel using the record-triggered flow
  • Validate Slack user authentication before posting a message
  • Use the decision element to validate the get records outcome 
  • and much more

Business Use Case

Martin Jones is working as a System Administrator at Gurukul on Cloud (GoC). His organization wants to post a message to a Slack Channel opportunities-notifications whenever an Opportunity is successfully closed and the opportunity owner has authenticated Salesforce & Slack. 

  • Message<tag opportunity owner>, just closed a new deal. congratulations!! 👏

Pre-requisites:

  1. The first step is to complete Salesforce for Slack integration.
  2. The next step is to set up message destinations for Slack channel opportunities-notifications.

What is a Slack Channel?

Slack organizes conversations into dedicated spaces called channels. Channels bring order and clarity to work –  you can create them for any project, topic, or team. With the right people and information in one place, teams can share ideas, make decisions, and move work forward.
In Slack, channels can be public or private.

Automation Champion Approach (I-do):

There are a few possible solutions for the above business scenario, but I’ll use After-save Record-Triggered Flow to solve the business requirement. 

Before proceeding ahead, you have to understand the following objects. 

Object Name Details
Message Destination

A Message Destination represents a conversation in Slack where Salesforce data will be shared. It consists of a specified Slack Workspace and Slack Channel. The destination information will be passed into the Send to Slack invocable method, which will then send Salesforce data to the specified conversation in Slack.

Slackv2 Auth OAuth 2.0 is a protocol that lets your app request authorization to private details in a user’s Slack account without getting their password. It’s also the vehicle by which Slack apps are installed on a team. Your app asks for specific permission scopes and is rewarded with access tokens upon a user’s approval.

Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes to go through the following Flow diagram and understand it. Let’s begin building this automation process.

Guided Practice (We-do):

There are 8 steps to solve Martin’s business requirement using Record-triggered Flow. We must:

  1. Define flow properties for record-triggered flow
  2. Add a decision element to check if an opportunity is successfully closed or not
  3. Add a get record element to find Slack message destinations for channel opportunities-notifications
  4. Add a decision element to check if the Slack message destination was found or not (from step 3) 
  5. Add a get record element to find Slack Auth for opportunity owner
  6. Add a decision element to check if Slack Auth for opportunity owner was found or not (from step 5) 
  7. Add a Text Template to construct the message body
  8. Add Slack: Post Message action  – to post a message to a Slack channel 

Step 1: Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows, then click on the New Flow.
  4. Select the Record-Triggered Flow option, click on Create 
    1. Object: Opportunity
    2. Trigger Opportunity Flow When: A record is created or updated
    3. Set Entry Criteria
      1. Condition Requirements: None
    4. Optimize the Flow For Action and Related Records
  5. Click Done.

Step 2: Using Decision Element to Check if Opportunity is Successfully Closed or Not

Now we will use the Decision element to check the StageName to ensure that it is equal to Closed Won. 

  1. On Flow Designer, click on the + icon and select the Decision element. 
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label; the API Name will auto-populate.
  4. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!$Record.StageName}
      2. Operator: Equals 
      3. Value: Closed Won

Step 3: Adding a Get Record Element to Find Slack Message Destination for opportunities-notifications Channel

This step aims to find the opportunities-notifications channel Id in Slack.

  1. On Flow Designer, below the Closed Won node, click on the +icon and select the Get Element element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Slackv2__Message_Destination__c object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: slackv2__Channel_Name__c
      2. Operator: Equals
      3. Value: opportunities-notifications
    2. Click Add Condition
    3. Row 2:
      1. Field: slackv2__Channel_Type__c
      2. Operator: Equals
      3. Value: channel
  6. How Many Records to Store:
    1. select Only the first record
  7. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  8. Click Done.

Step 4: Using Decision Element to Check If Slack Destination was Found or Not

Now we will use the Decision element to check the Record Variable from step 3 to find if it returns the Message Destination record for the Slack channel opportunities-notifications found or not. 

  1. On Flow Designer, click on the +icon and select the Decision element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute OutcomeAll Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!Get_Message_Destination}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
  5. Click Done.

Step 5: Adding a Get Record Element to Find Slack Auth for Opportunity Owner 

This step is optional, but we want to ensure the opportunity owner has authenticated the Salesforce app in Slack as per the requirement.

  1. On Flow Designer, below the Yes node, click on the +icon and select the Get Element element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Slackv2__Slack_Auth__c object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: slackv2__User__c
      2. Operator: Equals
      3. Value: {!$Record.OwnerId}
  6. How Many Records to Store:
    1. select Only the first record
  7. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  8. Click Done.

Step 6: Using Decision Element to Check If Slack Auth for Opportunity Owner was Found or Not

Now we will use the Decision element to check the Record Variable from step 5 to find if it returns the Slack Auth for Opportunity Owner found or not. 

  1. On Flow Designer, click on the +icon and select the Decision element.
  2. Drag-and-drop Decision element onto the Flow designer. 
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Under Outcome Details, enter the Label the API Name will auto-populate.
  5. Condition Requirements to Execute OutcomeAll Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!Get_Slack_Auth_Opportunity_Owner}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
  6. Click Done.

Step 7: Add a Text Template to Construct Message Body

 Now we’ll construct a Slack message body using the Text Template. The text template allowed you to specify the multi-line messages. You can tag a user in a Slack post by using the brackets around the @SlackuserID, like <@SlackuserID>. 

  1. Under Toolbox, select Manager, then click New Resource to create a text template. 
  2. Input the following information: 
    1. Resource Type: Text Template
    2. API Name: tt_MessageBody
    3. Select View as Plain Text
    4. Body 
      1. <@{!Get_Slack_Auth_Opportunity_Owner.slackv2__Slack_User_Id__c}> just closed a new deal. Congratulations!! 👏
  3. Click Done.

Step 8: Add Slack: Post Message action  – to Post a Message to a Slack channel 

We will use the Slack:Post Message action to send a message to a Slack channel. 

  1. On Flow Designer, below the Found node, click on the +icon and select the Action element.
  2. Search and select the Slack: Post Message from the dropdown menu
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Set Input Values
    1. Message Destination ID: {!Get_Message_Destination.Id}
    2. Record ID: {!$Record.Id}
    3. Message: {!tt_MessageBody}
  5. Click Done.

 In the end, Martin’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 55
  5. Interview Label: Post a message to a Slack Channel {!$Flow.CurrentDateTime}
  6. Click Save.

Almost there! Once everything looks good, click the Activate button.

Proof of Concept

Now onwards, if a business user updates the Opportunity Stage to Closed Won, record-triggered after-save will automatically post a message to the Slack channel.

  1. Navigate to the Opportunities tab and update the opportunity Pyramid Emergency Generators status to Closed Won, as shown in the following screenshot:
  2. Check out the opportunities-notifications Slack Channel.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

5 thoughts on “Post a Message to a Slack Channel using Flow

  1. Is there any reason you choose to use the legacy app opposed to the new “Send Slack Message (Beta)” core action? Asking as I’ve been struggling to get the new core action to work

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.