Monday, April 21, 2014

Salesforce Integration with Twilio

Hi guys,

  For instance ,whenever contact is created or updated,Customer can get the welcome call or any custom voice message.

 for this i created "CustomerType" Picklist,based on this picklist customer will receive the call.

 Values of Picklist: Gold,Silver

 For below example ,call will send only for "Gold" type customers.

 First of all we have to create an account on twilio website. Twilio website provides two type of account .

a.) Free account:-   In this type account you can call to any number around the world, but the number  from which you call should be registered with twilio.

b.) Paid account:-  In this type account you can purchase new number, can port your number with twilio and can send sms also. With this account you can also use many other premium facilities like you can create client app and can use interactive deshboard etc.

But for now we will create only free account as follow:-

a.) Open  https://www.twilio.com/try-twilio and create an account

b.) Verify Your number by request a call or sms and then enter code which you get from call

c.)   Now the twilio will give you a chance to select a number.

 Add remote site settings :  https://www.twilio.com

BatchApex: As Of now triggers are not supporting Callouts , so we are going to creating batchapex and executing using apex trigger.

Batch Apex: 

global class TwiloBatchapex implements  Database.batchable<Sobject>,Database.AllowsCallouts{

Public string query ='Select id,CustomerType__c from Contact';

global Database.QueryLocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator(query);
 }

     global void execute(Database.BatchableContext BC, List<Contact> records) {        
       String account = 'xxxxxxxxxxxxxxxxxxxxxxxx';
String token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
TwilioRestClient client = new TwilioRestClient(account, token);

Map<String,String> params = new Map<String,String> {
        'To'   => 'xxxxxxxxx',
        'From' => 'xxxxxxxxxxxx',
        'Url' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    };
TwilioCall call = client.getAccount().getCalls().create(params);
       }
 

    global void finish(Database.BatchableContext BC){
       //you can also do some after-the-job-finishes work here
    }

}

Trigger : 

trigger Sampletwilo on Contact (after insert,after update) {
 List<Contact> conList = new List<Contact>();
 for(Contact con : trigger.new){
     if(con.CustomerType__c =='Gold')
        conList.add(con);
 }
    if(conList.size() > 0)
      database.executebatch(new TwiloBatchapex(),200);
}

Hope so this will help you guys,

4 comments:

  1. HI Thanks For your help
    But TwilioRestClient client = new TwilioRestClient(account, token); where can i get this class..?

    ReplyDelete
  2. This was really an informative post, thanks for sharing the tips with us.

    Salesforce CRM Services

    ReplyDelete
  3. Nice blog. This blog is very useful for salesforce developer and will benefit them in many ways. Looking for more blogs like this. Thank you.

    ReplyDelete