How to send #sms using third party sms APIs from Salesforce ? | Send SMS from Salesforce

How to send SMS using third party sms APIs from Salesforce ?

In this blog I will explain how we can send SMS using a third party API service.

You can buy sms from any bulk sms provider, there are tons of bulk sms provider online. Just make sure they are providing API also.

First of all you have to add your bulk sms provider's website in Remote Site Settings. To add the website in Remote Site Settings please follow below steps : 

  1. From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
  2. Click New Remote Site.
  3. Enter a descriptive term for the Remote Site Name.
  4. Enter the URL for the remote site.
  5. Optionally, enter a description of the site.
  6. Click Save.
After adding remote site you can make the callouts to your service provider's website. For testing purpose I am creating a custom Visualforce page with a Sign Up button.

I will be sending the SMS on the click of Sign Up button.

To send the sms on button's click let's create an APEX class as shown below : 

sendSMSCTRL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Public class sendSMSCTRL{
    public sendSMSCTRL(){
        
    }
    public void sendSMS(){
        String msg = 'Hello';
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://yourwebsite.com/api/sendSMS.asp?username=YOURUSERNAME&password=XXXXX&sender=MYTEST&sendto=8890288555&message='+msg);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody('{"name":"testing"}');
        HttpResponse response = http.send(request);
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }
    }
}


In above code I am making a HTTP callout using POST method to my service provider's API url with all the required parameters.

You can also send the parameters as JSON encrypted format.

Now let's create a Visualforce page : 

sendSMS

1
2
3
4
5
<apex:page controller="sendSMSCTRL">
    <apex:form>
        <apex:commandButton value="Sign Up" action="{!sendSMS}" />
    </apex:form>
</apex:page>


In above VF page I have just created a button to send the SMS. On click of that button my VF page will call my Apex method sendSMS() and trigger the sms process.

You can add the parameters dynamically as per your requirement.

Please checkout video tutorial below :



If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 

Post a Comment

3 Comments

  1. Hello
    Please advise me how to register that API.

    ReplyDelete
    Replies
    1. Google for bulk sms service providers and register for any one of them.

      Delete