Tag Archives: salesforce

Use HTTP Callouts in Flow to send SMS via Esendex – Part 2

Welcome back, hopefully you’ve read part 1 of this post, if not you can find it here: Use HTTP Callouts in Flow to send SMS via Esendex – Part 1

In that we covered how to set up the External Service and Named Credentials and how to give the users access to these to use in your flow.

So to the flow itself:

I’ve built this for demo purposes as a simple screen flow and put it on my contact record.

2 fields, phone number so you can check its correct and Message to free type what you want to say

Lets start by taking a look at the Esendex message dispatcher API.

This is everything additional to the Authentication that we need to send them, We’ve already taken care of the Content-Type and Accept in the Named Credentials. (see Part 1)

We still need to pass the account reference and the message recipient(to) and the message(body).

You can if you want and need pass multiple to and body values, a collection if you will all at the same time. Esendex can accept up to 50,000 at one time if you need it to.

So back in the flow, we’ve grabbed the contact record that we are on’s Mobile number in the Get Mobile element,

We’ve presented a screen to enter the message

I’ve used a phone element from the standard components for a screen flow and set the default value as the mobile number from the get request.

As you can see in the flow above we’re going to need to assign some values, but what are we assigning them to? First we need to skip ahead slightly and build the Send SMS action.

Click to add a new element and select the Action option

Now we’ll create the HTTP Callout

Give it a name and description that is meaningful, I’m calling it SendSMS, the Esendex API has a number of different end points for different functions so lets make the name useful for later and then select the Named Credential that we created in Part 1, in my case this is Esendex.

Next we provide a little more description for the Call out, we select the Method, which in the instance of this Esendex End point is POST and the end point of the end point in this case /messagedispatcher as we included the start of the URL in the Named Credential.

We don’t need to include any additional Query Parameter keys.

Now this is where the magic starts, Salesforce needs to know the Json that it should be using to send the request, so we just copy the Json into the field and hit review.

{ 
"accountreference":"EX0000000",
 "messages":[
{ 
"to":"447700900123",
 "body":"Every message matters!" 
}] 
}

And its already worked out what the types are for the fields

Now we are going to do the same for the Response that Salesforce is to expect.

Because I’ve been using the samples from the Esendex documentation, and didn’t add my accounts EX reference to the Request sample, I’m using the Use Example Response.

If you choose Connect for Schema, and don’t enter your EX reference to the sample, Esendex will return an error.

Again copy the sample from the Esendex site.

{ "batch":
 { "batchid": "19aafc9c-b08a-400d-a9f1-afe3c46f0a25", "messageheaders": [ 
{ "uri": "http://api.esendex.com/v1.0/messageheaders/72d91006-eb01-4e69-ae98-0e02c92c8e34",
 "id": "72d91006-eb01-4e69-ae98-0e02c92c8e34" } ] 
}, 
"errors": null }

And it’ll have a go at setting the types, the only one it will struggle with this time is Errors because it is null in the example, so set this to String.

We’re nearly done with this wizard, one last thing to do is create the Variable which we will assign the request body to when ready to send.

Don’t confuse this with the body used in the Esendex request that is the body of the message, here we mean the whole set of data we are sending to Esendex.

As you normally would, click in the field and select New Resource, give it a Name and a description, everything else is completed for you.

If you want to you can select the Manually assign variables for capturing the responses, I’ve left it un checked for now, I imagine I might need these when I come to use this in production.

Now this is where is gets a little tricky, Salesforce has created all the apex that we need in the background, however some of it we can’t see.

If right now you go to create a new assignment, you’ll see that some variables already exist.

The output variable exists and it has an Arrow, so you can drill deeper and so does SMS Body.

But it only allows you to the top level of the nested array (Collection) of the body messages not the message

So we need to create a new variable.

New Resource > Variable

Select your API name

And for Data Type select Apex Defined, and now a new field appears to select the apex class, when you click in it will show all the classes available, scroll down, the one you need, will start with ExternalService and then has the API names that you gave when you created the Action earlier. And then the rest we are looking for is IN_body_messages

You can see mine in the image below

Now when we try to assign to variables we can see inside the nested array.

So now we can assign the values from the Screen to the single variables.

And then our account reference and our single message to our messages variable.

Add a final confirmation screen for you users outputting response code from Esendex, the Batch id if you need it, and any errors.

And now when you run the flow from your page layout like I have your SMS is sent.

This is the simplest send I could think of, in a production environment you might want to do more with dynamically sending the SMS, so you might want to start from a Record Trigger Flow rather than a Screen flow.

You might, and I suspect I will, want to create an object to record the Esendex batch id’s and the message id’s of the messages sent.

And remember that the Esendex API can accept up to 50,000 messages in a single batch, just by passing multiple copies of what I called the single message into the SMSbody. Think of the single message as a Record and the batch as a Collection. (Let me know in the comments if you’d like a walk through of this)

So thats it, how to send an SMS via Esendex in 2 hopefully easy parts, This was the first time I’ve used the HTTP Callout feature and I loved how easy it was to use with a Rest API. I hope it helps!

Use HTTP Callouts in Flow to send SMS via Esendex – Part 1

Those of you that know me, will know that I spent 9 and a half years as Customer Support Manager for Esendex, which is where I got my feet wet with Salesforce. One question that came up from customers that we couldn’t really help with was how to send from Salesforce. It didn’t come up often enough for us to invest in a solution, we generally had to point the customer to the API documentation and send them off to code it themselves. Which at the time would have meant APEX and a developer, now however we have HTTP callout in flow, which I recently used for the first time for another API based issue I needed to resolve, so out of curiosity I set to to see if I could make it work for Esendex.

SPOILER ALERT: I could.

For this I’m using their older API (https://developers.esendex.com/api-reference/#messagedispatcher) this being the one I supported, the newer one may be easier to implement.

Lets get started

The first thing I needed to do was set up a Named Credential and External Credential, but there are some tweaks here that I had to make to accommodate Esendex.

Their API (https://api.esendex.com/v1.0) by default is XML rather than Json, (the json elements were added after the development of it in XML) so we have to add some Custom Headers to tell it that we want to send and receive Json.

External Credential

By default you’ll be taken to the Named Credential tab, but you want to start with the External Credential, because the external is required by the Named.

And External is where the magic happens.

Esendex uses Basic Auth, there is a session endpoint, but its only documented in XML, no json examples listed so I didn’t bother trying, you might want to try, as it is better in terms of security than passing your auth with each request.

Lets get started and give it a name, I’ve named everything as Esendex, so I can find what I want easier.

I used the custom option, as the Basic Authentication option in flow is newer, and I know what I’m doing with it, again you might want to try it.

Then we create a Principal, I’m not a dev, I don’t know why its called principal.

Again I’ve called it Esendex.

Parameter 1 is Username and then the value is the username starred out.

Parameter 2 is Password and then the value is starred out, remember though with Esendex that your portal password is not your API password, you have to set your API password within your profile.

I’ll come back to that piece at the bottom about the permission set, first we need to set up the custom headers we need, there are 3.

  • Authorization
  • Accept
  • Content-Type

These are standard names for headers that you might send, if you tested this in Postman you’d select them from a drop down list, it’d be a nice touch if one day Salesforce added such a list.

First  in the sequence is Authorization

The value for this is a formula, so it ought to look familiar:

{!’Basic ‘ & BASE64ENCODE(BLOB($Credential.Esendex.Username & ‘:’ & $Credential.Esendex.Password))}

Esendex is my principle name

Username and Password at the Name I gave each parameter.

So if you named your principle SendSMS you would use $Credential.SendSMS.Username

That’s going to get you authenticated against Esendex’s API.

Now, do you remember how I said they defaulted to XML, and you need to tell them to use Json, that’s what the next two do, the order didn’t seem to matter, but I did seem to need both in my testing.

Name: Accept Value: application/json

Name: Content-Type Value application/json

So now your screen should look similar to this

Apart from the Related Named Credentials, yours will be blank.

Let sort that now.

Go back to Named Credentials.

Named Credential

You have your entry under External Credentials that we just created, so now on Named Credentials lets hit new

So again I’ve called mine Esendex, then in URL we enter the base URL for their API which is https://api.esendex.com/v1.0

So by entering the Base URL rather than the dispatcher URL we can use this later to do more than just SendSMS if we have the account options, if your account is set up to receive messages you could access your inbox, or most likely you’ll want to check the status of your sent messages.

In the External Credential, you can select the External Credential we just created, see why we did that first?

And most importantly, for that formula to work for the Authentication we need to check Allow Formulas in HTTP Header

It save and that is all the authentication piece set up nearly!

One last thing to do give users access to the Principle via a permission set, so they can sendSMS

So create a new permission set, call it what you will, again I’ve gone with Esendex, so I know what its giving access to.

Then in the Apps section, you are looking for External Credential Principle Access

Click Edit and move your principle from left to right

Save it and now assign it to whomever needs access this included your admins.

In Part 2 we’ll look at how to use the HTTP Callout to access to the Esendex API.