Engineering Apps for Reuse: A Practical Example Using Slack, MuleSoft, and Salesforce

Design, build, deploy, and monitor APIs

Dave Norris
Better Programming

--

ASCII art. The API to reuse across Slack and Salesforce | image by author

This article describes the process of designing and building an API that can be reused across Slack and Salesforce. It demonstrates how to create a simple slash command in Slack and a Flow in Salesforce to convert text to ASCII art.

Repurposing an existing API to connect different systems is one key metric that drives productivity and helps reduce costs within an organisation.

The Idea

Let’s say we are tasked with creating an interactive experience for users in Slack. We want to provide a slash command that allows users to specify some text and have our application convert that text into ASCII art.

There are three questions we should think about;

  1. Are there existing programming libraries that I can reuse?
  2. Is there an existing API I can reuse?
  3. How many clients might want to use this feature?

The Design

The first step was to design an API specification that can take a message as an input and output ASCII art.

By using MuleSoft’s Anypoint Platform I can not only design the API specification but share it internally to make it discoverable by others looking to add similar functionality to their apps.

Anypoint Design — Creating API specifications

My next job was to decide if there were existing programming libraries that can help me.

MuleSoft applications are based on Java and as one of the most widely adopted programming languages worldwide, I found a number of Java classes that could help me.

Now, Slack slash commands expect inputs and outputs to be in a Slack-specific format. Instead of encapsulating all the logic in one API call, I’ll split the text to ASCII conversion into a separate API that can be reused.

ascii-art-sapi will be based on our API specification. Designed to be reused.

slack-ascii-art-xapi will orchestrate and transform messages for Slack. In short, it will convert ASCII art to something that looks good in Slack.

Creating two APIs to set us up for success

To demonstrate the ability to reuse APIs we’ll also use the ascii-art-sapi to create the ability to convert text to ASCII in Salesforce.

In this case, we’ll use External Services to convert the API specification to invocable Apex that we can use directly in a Flow.

Reusing APIs across two different experiences

The Build

ascii-art-sapi

For the build phase, we are using MuleSoft’s Anypoint Studio. This provides connectors, processors, and modules that allow me to implement the API logic in less time than writing code.

Since MuleSoft compiles to a Java application I can include the jfiglet library as a dependency.

Including jfiglet as a dependency

Using a Transform Message module allows me to use the above Java class to transform an input to ASCII.

I can import the Java class from the dependency and then simply use the methods inside. In this case, convert a given string to ASCII characters.

Importing third-party libraries and calling their methods

We are now ready to deploy the API.

slack-ascii-art-xapi

For the slack-specific logic, we’ll build the following:

Slack-specific logic encapsulated into a new API

This API will receive the input from the Slack slash command and immediately publish it to a queue to process it asynchronously.

The asynchronous listener will then call the existing ascii-art API to convert the text from the slash command into ASCII before sending the message back to a Slack channel.

So, it looks like this:

Slash command — ascii-art

Okay, so we now have a functioning Slack slash command. Let’s look at getting this experience into Salesforce.

Since we have the ascii-art API designed we can use External Services to import the specification into Salesforce.

Named credentials are needed in Salesforce to provide the endpoint and authorisation details for the Anypoint Platform and ascii-art API.

From Salesforce navigate to Setup->External Services and walk through the wizard. In this example, we connect to the MuleSoft Anypoint Platform so that Salesforce can find and create Apex classes to call our ASCII art endpoint.

Creating an External Service in Salesforce

Now that we have the External Service created we can use declarative tools to invoke the API. In this example, we’ll use Flow.

Flow automatically includes the action needed to use the API we imported using External Services. We provided a screen for a user to specify the text to convert to ASCII and, in the example, above we save the output to a field on the Contact object.

At the end of the configuration, your user experience should look like the following:

Salesforce Flow calling an API endpoint using External Services

Summary

One of the key elements to engineering apps for reuse is the ability to design, build, deploy, manage and monitor APIs.

MuleSoft’s Anypoint Platform offers the ability to do just that. Couple that power with Slack and Salesforce and you can engineer some great experiences.

--

--

Developer Advocate @ Salesforce || Interested in solving unique challenges using different cloud service providers || All opinions are mine.