Skip to content

Instantly share code, notes, and snippets.

@kookoolib
Created March 20, 2012 15:49
Show Gist options
  • Save kookoolib/2137359 to your computer and use it in GitHub Desktop.
Save kookoolib/2137359 to your computer and use it in GitHub Desktop.
Salesforce Call management controller
/*The controller sends the XML response*/
public class CallManagementController {
/*state is required to know the call flow state*/
public string state { get; set; }
public string response { get; set; }
public CallManagementController() {
state='start';
}
/* return KooKoo XML */
public void kookooResponse() {
String resp;
if(state.equals('start'))
{
String dialNumber;
String name;
//Get the caller id
String fromPhone = ApexPages.currentPage().getParameters().get('cid');
if (fromPhone!=null) {
//get only the last 10 digits as we do not want the country code etc
fromPhone= fromPhone.length() <= 10 ? fromPhone : fromPhone.substring(fromPhone.length() - 10);
// search Lead and Contact phone number fields
List<List<SObject>> results = [FIND :fromPhone IN Phone FIELDS
RETURNING Contact(FirstName, LastName,Owner.Phone), Lead(FirstName, LastName,Owner.Phone)
LIMIT 1];
// extract the owner phone if there’s a match
if (!results[0].isEmpty()) {
//Found the caller as a contact. So get the owner of the contact and dial him
Contact r = (Contact)results[0][0];
name = r.firstName + ' ' + r.lastName;
if (r.owner!=null && r.owner.phone!=null)
dialNumber = r.owner.phone;
} else if (!results[1].isEmpty()) {
//Found the caller as a Lead. So get the owner of the Lead and dial him
Lead r = (Lead)results[1][0];
name = r.firstName + ' ' + r.lastName;
if (r.owner!=null && r.owner.phone!=null)
dialNumber = r.owner.phone;
}
else {
/*Did not find the caller in our system. So we will create a new lead. Play some message and hangup.
We could also dial to a default number or ask him to leave a voice mail etc.
*/
Lead l = new Lead(FirstName='Lead', LastName=fromPhone, Email='[email protected]', Company='test',Phone=fromPhone);
try{
insert l;
}catch(Exception e){
System.debug('System exception ' + e);
}
response = '<response><playtext>Hello new lead</playtext><hangup/></response>';
return;
}
}
state='dial';
//If owner is there for caller, we forward the call. Else, we hangup
if(dialNumber != null)
{
dialNumber = dialNumber.replaceAll('\\D','');
response='<response><playtext>Hello '+name+'.Please wait while we connect.</playtext><dial>'+dialNumber+'</dial></response>';
}
else
{
response='<response><playtext>Welcome back. Sorry, you have not yet been assigned an agent. Please try again</playtext><hangup/></response>';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment