Send SMS from Salesforce – Build Your Own

[ Update ]

We recently released an application on the AppExchange that provides the ability to send and receive SMS messages from Salesforce.  If you’re looking for a pre-built solution you can find it here – Simple SMS

Otherwise, if you’re just looking to flex your development muscle on the platform keep reading for how I built this older solution.

Ever send SMS messages to your Contacts and Leads?

I recently had a conversation with a fairly new Salesforce customer who wanted to send SMS messages to their leads and contacts.  They had been using another CRM system that gave them this ability and it’s an integral part of the way they communicate with leads and prospects.  The great thing about Salesforce and the platform is that there are multiple ways to achieve this goal.  A quick search of the AppExchange will bring up a number of options.  Or, if one of these options doesn’t fit your needs you could build it on your own.  With my curiosity peaked, I chose the latter option and decided to spend some time looking for a way to build this functionality myself.  Below, I’ll share with you the process and code that I put together.

Step 1 – Find an SMS Gateway company

According to Wikipedia, “An SMS gateway is a device or service offering SMS transit, transforming messages to mobile network traffic from other media, or vice versa, allowing transmission or receipt of SMS messages with or without the use of a mobile phone.”  If you Google around for “SMS Gateway” you’ll find a number of companies offering this service.  I decided on a company called Upside Wireless primarily because the pricing options appeared straight-forward and flexible, and there were API docs available for sending messages via SOAP and HTTP.  It only took a few minutes to set up a free account on their website.

**UPDATE: After creating your account with Upside Wireless, you’ll need to request API access to the account.  Visit their Contact Us page and use the form to request that API access be turned on.

Step 2 – Get the WSDL

We’ll use the SOAP method to send messages to the Upside Wireless web service, so the first step is to download the Upside Wireless SOAP WSDL here.  Then, create an APEX class from this WSDL by going to Setup-> Develop-> API -> Generate from WSDL.  You’ll need to do this in either a Developer or Sandbox account.  Parsing the WDL will generate some errors while because there are unaccepted items like multiple portTypes, etc that need to be removed.  It took a few attempts before I was able to complete the parsing successfully.  My final version of the WSDL file can be downloaded here.

**UPDATE: Upside’s API must have changed slightly since I posted this and the original WSDL will now throw an error due to a missing element.  You can skip the part of generating the class from the WSDL and just download the complete version of the web service class here.

Step 3 – The Basic Design

The basic design is to create a “Send SMS” button that resides on the Contact detail page.  When the button is clicked, a Visualforce page lets you type in the message and press send.  If the send is not successful, an error message will appear.  If the SMS is sent successfully, a task will be created to record this activity and the user will be sent back to the Contact’s detail page.

Here is the APEX Class and VF Page.  You’ll notice this is for Contacts, but it can essentially be duplicated for use with Leads.


public with sharing class SendSMSContactExtension {

    private final Contact contact;
    private final String TOKEN = 'My Token';
    private final String SIGNATURE = 'My Signature';
    private final String ENCODING = 'Seven';
    public String recipient {get; set;}
    public String message {get; set;}

    public SendSMSContactExtension (ApexPages.StandardController stdController) { = (Contact)stdController.getRecord();
    } //close constructor

    public PageReference sendSMS () {

       if (contact.MobilePhone != null) {

          //this reformats the phone number as required by the web service; removes all spaces and characters while adding the country code.  Currently
          //it's hardcoded to insert the U.S. country code prefix.
          recipient = '1'+ contact.MobilePhone;
          recipient = recipient.replace('-', '');
          recipient = recipient.replace('(','');
          recipient = recipient.replace(')','');
          recipient = recipient.replace(' ', '');

    	  //initialize the SOAP object and call the web service 'Send_Plain'.  Set the timeout.
          upsidewirelessComWebserviceSms.SMSSoap sms = new upsidewirelessComWebserviceSms.SMSSoap();
          sms.timeout_x = 2000;
          upsidewirelessComWebserviceSms.SMSSendResult result = sms.Send_Plain(TOKEN,SIGNATURE,recipient,message,ENCODING);

          //Create a new task, related to the Contact, if the response is successful.
          if (result.isOk == true) {
             Task smsTask = new Task (Type='SMS',
        							  WhoID =,
        							  Status = 'Completed',
        							  ActivityDate =,
        							  Description = message,
        							  Subject = 'SMS Sent'
        	 try {
        	    insert smsTask;
        	 }  catch (System.Dmlexception e) {
        	   	   System.debug('Error: Unable to insert task: ' + e);

        	 return new ApexPages.StandardController(contact).view();

          } else {
           	   //this message will be triggered if the text was not sent successfully.
        	   ApexPages.Message didNotSendMsg = new ApexPages.Message(ApexPages.severity.Info, 'Sorry, but the SMS did not send correctly.  Please try again.');
        	   return null;
             } //close else

       } //close first if-statement
          else {
    	     //this message will be triggered if the Contact doesn't have a mobile phone number.
    	  	 ApexPages.Message noMobileMsg = new ApexPages.Message(ApexPages.severity.Info, 'This Contact does not have a mobile phone number.  Please update the mobile number and try again.');
    	  	 return null;

    } //close sendSMS method

    public PageReference cancel () {
       //sends the user back to the Contact detail page.
       PageReference contactPage = new ApexPages.StandardController(contact).view();
       return contactPage;
    } //close cancel method

} //close Class


<apex:page standardcontroller="Contact" extensions="SendSMSContactExtension" tabStyle="Contact" >
 <span style="font-weight:bold; color:red">
 <apex:messages />
 <apex:form >
 <apex:pageBlock title="Send SMS to {!}">
 <apex:pageBlockSection >
 <apex:pageBlockSectionItem >
 <apex:outputLabel for="message" value="Enter your message:" />
 <apex:inputText id="message" value="{!message}" size="80" required="true" />
 <apex:pageBlockSectionItem >
 <apex:outputLabel for="recipient" value="Mobile Number" />
 <apex:outputText id="recipient" value="{!contact.MobilePhone}" />
 <apex:pageBlockSectionItem >
 <apex:commandButton action="{!sendSMS}" value="Send" id="sendButton" />
 <apex:commandButton action="{!cancel}" value="Cancel" id="cancelButton" />

In the APEX class you’ll notice two variables called Token and Signature. These are used for authentication by Upside Wireless and are passed in as arguments to the Send_Plain method.  To get your Token and Signature, go here and then enter your username and password and click Invoke – obviously you’ll need to have created your account prior to doing this.  Copy and paste your Token and Signature into the class.

Step 4 – Create the Button

To create the Send SMS button, go to Setup-> Customize -> Contacts -> Buttons and Links.  Create a new detail page button called ‘Send SMS’.  For Content Source select Visualforce Page.  Then, select the page from the drop-down menu.

Go to Setup -> Customize -> Contacts -> Page Layouts.  Select your page layout and click Edit.  Add the Send SMS button to your Custom Buttons section.

Step 5 – Add Remote Site Setting

Before you can send SMS’s, you’ll need to add the Upside Wireless web service endpoint to your remote site settings.  Go to Setup-> Security Controls-> Remote Site Settings.  Add a new Remote Site with this URL address –

Step 6 – Send SMS

Now you’re ready to send SMS’s from any Contact detail page.  Just make sure that you have a mobile number for the Contact.  When opening your free account you do have a small amount of SMS credits to work with.

I’d suggest reading through the API docs to familiarize yourself with the available methods regarding testing, sending through a dedicated phone number, etc.

This is just a rudimentary design primarily for the purpose of demonstrating capabilities.  However, it is another testament to the power of the platform and the ability to quickly put together something that works.

I hope this has been helpful and, as always, would be happy to hear any feedback.

Tags: , , , ,

Send SMS from Salesforce – Build Your Own

33 Responses

  1. Nice walk through!!

    I just clicked send and got this permission error:
    Web service callout failed: WebService returned a SOAP Fault: System.Web.Services.Protocols.SoapException: No Permission to: sms.send.plain at SMS.Send_Plain(String token, String signature, String recipient, String message, SmsEncoding encoding) faultcode=soap:Client faultactor=

    My token and signiture are correct. And I can’t think of any other permission issues.

    Any ideas welcome!