Using Slack Command to Lookup Opportunities in Salesforce

In this tutorial we are going to build a pipeline that listens on new messages in Slack that start with “search:”. It then searches Salesforce Opportunities that have matching names and posts selected information back to the Slack channel.

How to do it

Let’s start by creating a new, blank pipeline by clicking the Create a Pipeline button on the dashboard.

../_images/create-a-pipeline-button.png

The first pipe that needs to be added to the pipeline is a trigger, that will fire whenever a new message is posted to Slack. We need to locate Slack in the channels list on the right, then drag and drop the “New Message” trigger.

../_images/slack-new-message.png

After dropping the pipe in the first slot of the pipeline, select your account (or connect a new one if needed), then select the Slack channel you are interested in monitoring.

../_images/slack-new-message-pipe.png

Here we want ot act on Slack messages that start with “search:” so we need to add a filter to the pipe. Filters make sure that only events that match will fire the trigger and execution of the pipeline will continue. Filters are supported on pipes that have the Add filter button near the end (blue plus icon)

Adding a Filter

Click the Add filter button (blue plus icon) and select Text as the field to filter on.

../_images/slack-new-message-pipe-filter.png

We are only interested in messages that start with “search:”, so in the filter condition drop-down select starts with and in the filter value enter “search:”. This will make sure that the trigger will fire only on messages that start with “search:”

../_images/slack-new-message-pipe-filter2.png

Now, since the New Message pipe outputs the entire message that was sent to Slack, we need to split it into two parts - “search:” which we’ll call “command” and the string that was searched for, which we call “term”.

Splitting Text With a Regular Expression

In order to do that we will use a Regular Expression. Regular Expressions (also called “regexes”) are extremely flexible and powerful and allow you to do all kinds of magic with text. In our example we are going to use one to split the message we have from Slack into two parts – the “command” used (“search” in this example) and the search term.

Locate the “Apply Regular Expression” pipe under Built-in channels -> Text and add it as the next step in the pipeline.

../_images/regular-expression.png

Apply Regular Expression has only two parameters – Text, the text to apply the regular expression on, and Regular Expression, the regex to apply. Since we are interested in splitting the Slack message from the fields list drag and drop Text.

../_images/regular-expression-dnd.png

In the Regular Expression textbox enter: (?P<command>.*)\:\s(?P<term>.*)

This will make sure that in the pipe output we will have two named fields - command and term. We consider all that’s before the colon in the Slack message to be “command” and any characters after to be “term”.

Note: For a reference of the supported syntax please refer to this document. You can also use Pythex to test your regular expression.

After you are done the “Apply Regular Expression” pipe should look like this :

../_images/regular-expression2.png

At this point we now have the message that was posted in Slack split in command and term which are available in the output of “Apply Regular Expression”. Note that because of the filter that we added in the “New Message” trigger the command will always be “search”.

So now all that’s left is to search Trello and post back to Slack.

Searching Opportunities in Salesforce

Select the All Channels tab on the channels list and locate Salesforce. Note that you can also use the text-box above to search by name for speedier access. Then add the Search Objects (SOQL) pipe as the next step in the pipeline :

../_images/salesforce-search-users.png

In the Search Objects (SOQL) pipe select the Salesforce account that you want to use or if needed connect a new one. For Object Type select Opportunity from the drop-down and in the Query textbox enter: WHERE Name LIKE '%{{b:term}}%'

../_images/salesforce-search-users2.png

Having Search Objects configured like this will search for Opportunities that contain our term search field in their Name property.

The last thing we need to do is to post back to Slack the results we’ve found.

Posting to Slack

Adding a Search pipe automatically adds a for-each iteration block after it. Using this we can act on each individual result found.

../_images/salesforce-foreach.png

So now all we need to do is add a Post Message action pipe from Slack. In the channels list locate Slack. Drag the Post Message pipe and put it in the slot after DO.

../_images/salesforce-slack-post1.png

You can now select a specific channel in your Slack that you want the results posted to. Alternatively drag and drop ID from the list of fields to the right after expanding the first list of fields (the one from the “A” pipe).

From the fields list drag and drop the fields you want posted to Slack in response to our query. Note that since we are posting to Slack we can make use of the markdown-like formatting Slack supports.

../_images/salesforce-slack-post2.png

For our example in the Text textbox we are going to enter this:

*name: {{c:name}} ({{c:probability}})*
  *amount:* {{c:amount}}
  *stage:* {{c:stage_name}}
  *last activity:* _{{c:last_activity_date}}_
  *descr:* "{{c:description}}"
*---------------------------------------*

As you can see we’ve added several fields from Salesforce’s Opportunity result and used Slack formatting.

One optional step we can take is to add one more Post Message pipe to the very end of the pipeline that simply says “— End List —”, so even for the cases when the search returns no results you will get a note in Slack and know that it was an empty list.

Conclusion

This is all there is to it really. In this brief tutorial we’ve covered all that’s needed to build quite a sophisticated pipeline that automates your workflow for Salesforce by using Slack to search for Opportunities and query data. We’ve used a regular expression to correctly split “commands” in Slack and also filters to only act on commands we are interested in.

Should you need any assistance, as usual just drop us a line at support@cloudpipes.com or reach us in the in-app chat.

Happy integrating with Cloudpipes!