1

I've Install the twilio API in salesforce by using this Link https://login.salesforce.com/packaging/installPackage.apexp?p0=04t1a000000AQzf

and configure the custom setting in salesforce with Account SID and Token.

Create a sample VF page to send SMS. When i click on send button, its not showing any error, But I'm not receiving any message from twilio.

As I'm using Trial version of twilio, thus sender number is verified in twilio.

VF PAGE

<apex:page standardController="Account" Extensions="Send_SMS_Controller"
showHeader="false" sidebar="false">
<apex:form >
   <apex:pageBlock id="msgs"> 
        <apex:pageMessages ></apex:pageMessages>
         Body : <br/>
        <apex:inputText id="smsBody" value="{!smsBody}"   
 style="width:400px; height:50px;"/>
               <br/>
        <apex:commandButton action="{!send}" value="Send" 
id="sendButton" reRender="msgs" rendered="{!hasNumber}"/>
        <apex:commandButton action="{!getReturn}" value="Return to Account"
id="returnButton" reRender="msgs" rendered="{!NOT(hasNumber)}"/>
     </apex:pageBlock>

Controller:

public class Send_SMS_Controller {
public String smsBody {get; set;}
public boolean hasNumber {get; set;}
private final String  fromNumber = '+13343779137';// get it from twilio
//after login click Phone Numbers tab in left sidebar

private string dialCode = '+91'; // Add your dial Code of your country.
public Send_SMS_Controller(ApexPages.StandardController controller) {
hasNumber = true;
}
 public PageReference Send()
{
 String account = '***************'; // Account SID on home tab
String token   =   'd7e5688cde62d1f27c2aeaed9f03aa3a';//AUTH Token on home 
tab
TwilioRestClient client = new TwilioRestClient(account, token);

Id accountId = ApexPages.currentPage().getParameters().get('id');
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
Account a = [select Phone from Account where Id = :accountId];

if(a.Phone != null)
{
    String phoneNumber = a.Phone;
    if(!phoneNumber.Contains(dialCode))
    phoneNumber = dialCode+phoneNumber;// If by default dial Code is not 
    //on Phone number we will ad it.
    System.debug('phoneNumber'+phoneNumber);
     Map<String,String> params = new Map<String,String> {
    'To'   => phoneNumber,
    'From' => fromNumber,
    'Body' => smsBody
     };
     TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
}
else
{
     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
'This Account have No Phone Number Click Return to Account'));
     System.debug('Message is set');
     hasNumber = false;
     return null;
}

pagereference redirect = new PageReference(baseUrl+'/'+accountId);
return redirect;
}
public PageReference getReturn()
{
return new 
PageReference(URL.getSalesforceBaseUrl().toExternalForm()+'/'+ApexPages.currentP
 age().getParameters().get('id'));
 }}

Reference Link

0

Did you find your answer? It is possible you need to set the Geo settings to allow sms to that region

Messaging Geographic Permissions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.