0

I've install the twilo library in salesforce from Github by using quick install link. and create Sample VF page as below;

<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>
   </apex:form>

And controller is

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 dialCode 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.currentPage().getParameters().get('id'));
 }

}

Its throwing me error,

The From phone number +13343779137 is not a valid, SMS-capable inbound phone number or short code for your account.

Above no. is verified, as i'm using trial account. enter image description here

Please suggest me solution to resolve the error.

1

Please check out this link

SMS Error

You are using a trial account which means your from number should be +15005550006 and to number should be your verified number(the number you want send SMS to)

Test Credentials

If you received this error while trying to authenticate with your Test Credentials, you probably tried to send a message with a From number from your live account. The only number that can be used to send successful messages with your Test Credentials is +15005550006. For more information, read our documentation on the From number for sending SMS with your test credentials.

  • Thanks now error is resolved, but I'm not receiving any message from twilo. The noumber to which I'm sending message is verified in twilo. – Amol Jul 14 '17 at 9:43

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.