Twilioforce – Click to Call Demo

November 11, 2009 Appirio

To help you with your entries for the Twilio Developer Challenge that runs until Dreamforce I’ve put together a quick demo of a Click To Call function for Salesforce CRM. Basically, the demo adds a link to the Contact Page Layout and will connect your phone to the Contact’s Phone with a simple click.

First, download the Twilioforce library. It’s a good set of starter code. I had to fix a few defects to get this demo working, so the SVN trunk has been updated with the latest code. In addition to the code, you’re going to need a few custom fields to get this to run. I created an unmanaged package to make that easier.
Before you get started, create a Sites entry for the Visualforce Page called TwilioDialer. Add the controller and the Twilioforce classes to the public access list.

Take a look at the controller class before we fill in the configuration data. We’re using the TwilioRestClient and TwilioRestResponse classes from the Twilioforce library to make our calls a bit more efficiently.

public PageReference ClickToCallAction() {
TwilioRestClient client = new TwilioRestClient(CONFIG.AccountSID, CONFIG.AuthToken, null);
Map params = new Map();
params.put('Caller', CONFIG.OutGoingCallerID);
params.put('Called', TwilioTarget);
params.put('Url', CONFIG.ClickToCallURL + '?Called=' + EncodingUtil.urlEncode(, 'UTF-8'));

TwilioRestResponse response;
try {
response = client.request('/'+CONFIG.APIVERSION+'/Accounts/'+CONFIG.AccountSID+'/Calls', 'POST', params);
ReturnCode = 'Called Placed: Connecting ' + TwilioTarget + ' with ' + contact.Phone;
return null;
} catch (Exception e) {
ReturnCode = 'ERROR: ' + e;
return null;

1) Click on the TwilioConfig tab and create a new record. (The demo has a LIMIT 1 query so you can only have a single working config record).
2) Enter your Twilio developer info. Add an OutGoing Caller ID number that Twilio has validated.
3) Enter the URL for the Sites entry you created above.
4) Go to your User record and add your preferred phone number for Twilio calls

The package has a new page layout for Contact. If everything was done correctly you should see something that resembles the following illustration:

Clicking on the number will kick off the following events:
1) Make a REST call to Twilio’s /Calls method requesting an outbound call to the phone number you entered on your User account record. (Sending the Sites URL for callback)
2) Twilio places the call (and greets you w/ the demo script if you haven’t set up your paid account yet) and fetches the TwiML in the Sites Page.
3) The element is read and the Contact’s Phone (which was passed in the parameter string) is dialed from your phone.

Hopefully, this will give you a headstart on your developer challenge application. Good luck and let me know if you have any questions.

Previous Article Robot for Google Wave Robot for Google Wave

Jeff Douglas This is a small Google Wave robot that interacts with to display contacts, acco...

Next Article
Twilioforce – Autoinstall Unmanaged Package
Twilioforce – Autoinstall Unmanaged Package

Last week we posted about the Twilio developer challenge for We just updated the Twilioforce cod...