Create on-demand Slack Channels using Platform Event — Triggered Flows in Salesforce

Sombir Sheoran
7 min readAug 31, 2022

Understanding how can we leverage custom Platform Events to create a Slack channel on demand from Salesforce UI using Platform Event — Triggered Flow

Before you start: 💁‍♂️
✍️ In case you’re looking for a solution to automatically create a Slack channel from Salesforce on events like Create/update of any Salesforce object (say Account) with some conditions, then please go through my previous article (read here). 💡 Make sure all the pre-requisites are taken care before implementing the solution mentioned in this article.

The current article talks about a different solution approach where we need to create a Slack channel on demand from Salesforce, let’s say with a button click, in order to reduce the number of Slack channels getting created by automation. We’ll create a Slack channel only if needed by the business so that Slack is not flooded by channels for each Salesforce account.

Business Use Case 💡

  • Business needs that, instead of creating a Slack channel automatically, every time when a new Account record is created in Salesforce (including some filter criteria), we have to control the number of Slack channels.
  • So, they propose to create the Slack channel on-demand, as and when needed from Salesforce. They expect to put some button on the UI of the Account object in Salesforce (scenario can change for each business) so that whenever that button Create Slack Channel’ is clicked on a Salesforce account record, then only the Slack channel should be created.
  • Once the Slack channel gets created, we have to show a toast message in the Salesforce UI that the Slack channel with the given name got created successfully. Show errors as applicable if the channel name already exists in Slack.

Building the solution 👨‍💻

  • First thing first, since the Lightning web component is the latest UI framework for Salesforce, so we would be creating a simple LWC which holds the button Create Slack Channel’.
  • On the button Create Slack Channel’ click, we would be creating a custom platform event Messenger_for_Slack__e record using Account Id and Name field values.
  • A new record for the platform event Messenger_for_Slack__e would eventually launch our ‘Platform Event — Triggered Flow’ in Salesforce.
  • This ‘Platform Event — Triggered Flow’ in Salesforce would be creating the Slack channel using the ‘Create Slack Channel’ core action for Slack in flow.
  • For Success/Failure notifications in Salesforce from Slack, we again need to consume the response in another platform event: Slack_Messenger_Updates__e holding the new Channel_Id__c, Channel_Name__c, Error_Message__c if any and Is_Success__c.
  • Finally, we parse the response/ received from Slack in this custom platform event Slack_Messenger_Updates__e using JavaScript promises.
  • As per the response received from Slack, Show a success or error Toast Message in Salesforce UI.

Technical Implementation ⚙️

  • Go to Salesforce > Setup > Platform Events > Create
  • First, create a new custom platform event Messenger for Slack (Messenger_for_Slack__e) with custom fields Account_Id__c and Account_Name__c to hold Salesforce record’s Account Id and Name.
  • This platform event will eventually call the ‘Platform Event Triggered Flow’ to create a Slack channel.
New Platform Event: Messenger_for_Slack__e
  • Second, we would create one more platform event ‘Slack Messenger Update’ (Slack_Messenger_Updates__e) to receive the response from the JavaScript promise (JS file in LWC), from the Slack channel creation operation in the Flow.
  • This platform event will hold the Channel_Id__c, Channel_Name__c, Error_Message__c if any and Is_Success__c for the new Slack channel record. Add/Remove these fields as per your business need.
New Platform Event: Slack_Messenger_Updates__e
  • Then go to Setup > Flows > Create New
  • Select Type = ‘Platform Event — Triggered Flow‘ based on the platform event object: Messenger_for_Slack__e and
Creating new Flow
  • Now, create a Slack channel using the core action ‘Create Slack channel’ for Slack in Salesforce.
  • Select your appropriate Slack app, and Slack workspace name, and execute the action as, the formula value of Slack channel name, channel type, etc. Make sure the prerequisites mentioned in my previous article are taken care of, otherwise you might not see Slack App or workspace in the dropdown.
Create New Channel in Slack
  • Now, based on the Success/ Failure on the channel creation in Slack step, add the ‘Create Records’ steps in the flow to create the record for the 2nd platform event Slack_Messenger_Updates__e for both success and failure scenarios.
Create (2nd platform event) ‘Slack_Messenger_Updates__e’ record step
  • The Success path record creation of Slack_Messenger_Updates__e is mentioned below. These would be used in the LWC JS file to parse and display the response.
  • The same applies to the Failure path in the flow (passing Is_Success__c = false) and the fault message.
Success path for Channel creation
  • Optionally, we can query the Account Team Members object based on the Account Id field value, in case we have a requirement to Invite/Add those Account Team Members to the newly created Channel in Slack.
  • The final version of the flow should look something like the below:
Platform event triggered flow based on Messenger_for_Slack__e
  • Save and activate the flow. Take care of proper error handling in actual project-related flows for Production usage.

Creating the LWC for Create Slack Channel ⚡⚙️

  • Create a new LWC component with a single button ‘Create Slack Channel’
LWC = html file
  • Also, we would be needing a simple Apex @AuraEnabled method sendData2Slack to create the platform event Messenger_for_Slack__e record using Account Id and Name and this method to be called within the JS file of the LWC.
Apex class method to create platform event Messenger_for_Slack__e record
  • JS function handleSlackCall() called on button click from HTML file which calls sendData2Slack() method from above apex class.
Calling sendData2Slack method with Account Id and Name
  • The Above code will create a platform event Messenger_for_Slack__e record which will eventually call our ‘Platform Event — Triggered Flow’ in Salesforce.
  • Above Flow creates a Slack Channel and based on Success/Failure, creates a 2nd platform event Slack_Messenger_Updates__e record to parse the response from the Slack in JS file (handleSubscribe() function, see above screenshot)and accordingly show Success/Failure toast message with the new Channel name.
handleSubscribe() function in JS
  • Below modules must be imported to use ShowToastMessage and subscribe, unsubscribe from the platform events
Import Useful modules in JS
  • In the end, make sure in the meta-file of LWC has the property isExposed = true, and required <targets> are set to use them in Salesforce.
  • Add the above LWC to any account page where you want to test.

Testing the implementation end to end 🧪

  • Go to Salesforce, Accounts tab and create a fresh Account record for testing (optionally, open any existing account record)
  • Make sure from the Account > Edit Page, that the newly created LWC component is added to the Account record under test from the app builder. Now, you should be able to see the ‘Create Slack Channel’ LWC.
Create a Slack Channel LWC component on the Account record page.
  • Click on the ‘Create Slack Channel’ button. Since this was a fresh account record, there should not be any existing Slack channel with the same name. Hence. we get a success response from Slack and a success toast message is displayed in the Salesforce UI.

🎉🥳🎊 Congratulations, the new Slack channel got created.

Success
  • Click the ‘Create Slack Channel’ button again. Since there is already a Slack channel available with this name this time toast message with an error is thrown: Slack channel already exists.
Slack Channel already exists error
  • Now, to verify, go to Slack workspace, channel > browse channels, and sort by newest created channel.
Browse all Slack channels

🎉🥳🎊Congratulations, the new channel got created in Slack.

Well, it confirms that “Create on-demand Slack Channels using Platform Event — Triggered Flows in Salesforce” is working perfectly.❤️

Related Articles from the same author:

Developer details:

Please reach out to me for any suggestions or feedback. Would love to hear them❤ If you loved this post, please applaud at the end of this post with a 👏

--

--

Sombir Sheoran

I'm a Certified Salesforce Consultant ☁️ who loves to write technical blogs, which help simplify Salesforce solutions. Follow to learn more ✨⚡