I have install the Twilio package in Sandbox and I have register the mobile number in Twilio it generates new number. Then I need to get the SMS and create a new case in Sandbox.Where will I get the SMS details in TWILIO API from Sandbox. If customer send a SMS then API get the SMS details then create a new case in the Sandbox.

Please help me....


If it is the Twilio Helper Library for Salesforce that you ave installed, then it looks like you need to use the TwilioRestClient.getAccount().getSmsMessages().getPageData API to get the SMS messages.

It looks like you will have to poll i.e. make the call periodically and you can get Apex code run in that fashion by using the Apex scheduler. It is up to you to decide what details to take from the SMS and put in which fields of the Case.

Also see https://salesforce.stackexchange.com/questions/40850/how-to-receive-an-sms-in-twilio-and-from-twilio-api-revert-back-the-sms-to-custo.

The code would look something like this:

global class SmsToCase implements Schedulable {
    global void execute(SchedulableContext sc) {
        TwilioRestClient client = new TwilioRestClient(...);
        Case[] cases = new Case[] {};
        for (TwilioSms message : client.getAccount().getSmsMessages().getPageData()) {
            cases.add(new Case(
                    Subject = 'From SMS',
                    Description = message.getBody(),
                    Status = 'New'
        insert cases;


Filtering values (see Messages List Resource) can be supplied to limit the data returned:

String formattedToday = Datetime.now().format('yyyy-MM-dd');
Map<String, String> filters = new Map<String, String>{'DateSent', formattedToday};
for (TwilioSms message : client.getAccount().getSmsMessages(filters).getPageData()) {
  • Where I should write piece code in the Apex class or/trigger can you please tell me. – Ramesh S Jun 18 '14 at 8:09
  • @Ramesh It would be an Apex class - see the one I've added. But also see the answer BritishBoyinDC has just posted - whether to roll your own or use that depends on your specific requirements. – Keith C Jun 18 '14 at 8:44
  • Yes I have add the same piece of code pass the parameters accountID,AuthId then run the scheduled.finally I send an sms to +1901881xxxx unable to create case in sandbox.I am doing correct way or not please tell me – Ramesh S Jun 18 '14 at 10:02
  • I can't debug this for you - use these debugging techniques How do I start to debug my own Apex code?. – Keith C Jun 18 '14 at 10:05
  • I have written some debug statements in "SmsToCase" class before run the Schedulable it can't print anything – Ramesh S Jun 18 '14 at 10:42

This GitHub library I wrote will do that - you enter the Twilio details in a Custom Setting, expose a public Apex Rest Service via Force.com Sites, and tell Twilio to post to that URL. SF will receive the Twilio post about the SMS, look for an existing contact/case, and if it doesn't find one, create a case...

EDIT: Just to add more explanation to the worflow, you expose the smstoApex class as a public REST api via Force.com Sites - which provides the URL you use in Twilio.

That class in turn passes the details of the SMS to this processing class which parses the SMS from the Rest Request params (From Number, To Number, Message Body). It then searches for a contact based on From Number, and then cases for that Contact to determine if it is a message for an existing case, or if it requires a new case to be created. It then writes all the details to an SMS_Message_Tracking__c custom object, and sends the Id via email to this processing class. Why the email? Because the Sites user can't write to Chatter - and we use Chatter on cases to 'Thread' the messages for a case. So we send it to that class via an email service so it is processed by the a user with Chatter read/write privileges so we can use Chatter to alert call agents/display message thread.

If you don't want to use Chatter, you can just have the Sites user do all the processing...it can create/edit cases.

This repo actually uses Email to Salesforce to enable Chatter to work on the Cases, but you can modify the inbound handler class to just create a case directly if that is easier

  • Hi ,@BritishBoyinDC I need to create a case while customer send as sms via twilio.The case is created in salesforce .I am sent an sms to other's by using twilio api but unable to receive the sms. – Ramesh S Jun 22 '14 at 11:32

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.