Handling Incoming SMS using Apex

July 03, 2014

Look-up on Incoming SMS

This trigger is required when the mobile numbers are not present under standard Salesforce format – (210) 871-6148. For e.g you have mobile  numbers stored in different formats like 1234567890 or 123-456-7890 or 12345-67890. In this case you will have to write a trigger that picks up mobile number from the incoming SMS and converts it into these different formats for doing a look-up. We can do look-up in 2 ways one is using SOQL and another is using SOSL. Note: smagicinteract is the package prefix and is different for different packages. Please change it accordingly -

Package

Version

Package prefix

SMS Magic Interact

1.23

smsmagicvc1

SMS Magic Interact

1.36

smagicinteract

Test Class for Above Trigger Code
Using SOQL
SOQL is “Salesforce Object Query Language”. We can do lookup using this for an incoming mobile number. Sample code given below:
trigger RelateWithContactAndLeadsTrigger on smagicinteract__Incoming_SMS__c (before insert){
List contactList = new List();
List leadList = new List();
String senderMobileNumber = null;
for (smagicinteract__Incoming_SMS__c sms : Trigger.new){
senderMobileNumber = sms.smagicinteract__Mobile_Number__c;
if(senderMobileNumber.length() ==12 && senderMobileNumber.substring(0,2) == \'44\'){
senderMobileNumber = senderMobileNumber.substring(2,12);
}
if(senderMobileNumber.length() ==10){
senderMobileNumber = \'0\'+senderMobileNumber;
}
List<List> contactList = [FIND :senderMobileNumber IN ALL FIELDS RETURNING Contact(Id)];
Conatact conList = contactList [0];
if(conList.size() > 0){
sms.Contact__c = conList[0].Id;
}
}
}
Test Class for Above Trigger Code
@isTest
private class TestForRelateWithContactAndLeadsTrigger{
static testMethod void triggerForWithCountryCode() {
Contact con = new Contact();
con.LastName = \'Test Last Name 1\';
con.MobilePhone = \'441234567890\';
insert con;
smagicinteract__Incoming_SMS__c incomingSMS = new smagicinteract__Incoming_SMS__c();
incomingSMS.smagicinteract__external_field__c = \'test\';
incomingSMS.smagicinteract__Inbound_Number__c = \'987654321\';
incomingSMS.smagicinteract__Mobile_Number__c = con.MobilePhone;
incomingSMS.smagicinteract__SMS_Text__c = \'test SMS\';
insert incomingSMS;
List <smagicinteract__Incoming_SMS__c> incomingList = [select Id, Contact__c, smagicinteract__Mobile_Number__c from smagicinteract__Incoming_SMS__c where smagicinteract__Mobile_Number__c =: con.MobilePhone];
system.assertEquals(incomingList[0].Contact__c,con.Id);
}
static testMethod void triggerForOnlyMobileNumber(){
Contact con = new Contact();
con.LastName = \'Test Contact 2\';
con.MobilePhone = \'1234567890\';
insert con;
smagicinteract__Incoming_SMS__c incomingSMS = new smagicinteract__Incoming_SMS__c();
incomingSMS.smagicinteract__external_field__c = \'test\';
incomingSMS.smagicinteract__Inbound_Number__c = \'987654321\';
incomingSMS.smagicinteract__Mobile_Number__c = con.MobilePhone;
incomingSMS.smagicinteract__SMS_Text__c = \'test SMS 2\';
insert incomingSMS;
List <smagicinteract__Incoming_SMS__c> incomingList = [select Id, Contact__c, smagicinteract__Mobile_Number__c from smagicinteract__Incoming_SMS__c where smagicinteract__Mobile_Number__c =: con.MobilePhone];
system.assertEquals(incomingList[0].Contact__c,con.Id);
}
}
Using SOSL

SOSL is “Salesforce Object Search Language”. We can do lookup using this for an incoming mobile number. Sample code given below:

trigger RelateWithContactAndLeadsTrigger on smagicinteract__Incoming_SMS__c (before insert){
List contactList = new List();
List leadList = new List();
String senderMobileNumber = null;
String senderMobileNumber2 = null;
String senderMobileNumber3 = null;
for (smagicinteract__Incoming_SMS__c sms : Trigger.new){
senderMobileNumber = sms.smagicinteract__Mobile_Number__c;
if(senderMobileNumber.length() ==12 && senderMobileNumber.substring(0,2) == \'44\'){
senderMobileNumber = senderMobileNumber.substring(2,12);
}
if(senderMobileNumber.length() ==10){
senderMobileNumber = \'0\'+senderMobileNumber;
}
senderMobileNumber2 = senderMobileNumber.substring(0,5) + \' \'+senderMobileNumber.substring(6,11);
senderMobileNumber3 = senderMobileNumber.substring(1,11);
contactList = [select Id, OwnerId, Name from Contact where MobilePhone = :senderMobileNumber or MobilePhone =:senderMobileNumber2 or MobilePhone = :senderMobileNumber3];
if(contactList.size() > 0){
sms.Contact__c = contactList[0].Id;
}
}
}
Create Task on Incoming SMS

(Can also be used to show incoming SMS in Activity history)

trigger CreateSMSActivityOnIncomingTrigger on smagicinteract__Incoming_SMS__c (after insert) {

try{
Date todaysDate = system.today();
List taskList = new List();

for(smagicinteract__Incoming_SMS__c sms : Trigger.new){
Task newTask = new Task();
Contact contact = [select Id,OwnerId from Contact where Id =: sms.Contact__c];
User userData = [select Id, Name, Department from User where Id =: contact.OwnerId];
String recordType = null;
String department = null;
if(userData != null){
department = userData.Department;
}
if(department == null){
department = \'Business\';
}
recordType = department + \' Task\';
RecordType record = [select Id from RecordType where Name = :recordType];
if (record == null){
recordType = \'Business Task\';
record = [select Id from RecordType where Name = :recordType];
}
newTask.RecordTypeId = record.Id;
newTask.OwnerId = contact.OwnerId;
newTask.Status = \'Completed\';
newTask.Subject = \'SMS from number \'+sms.smagicinteract__Mobile_Number__c;
newTask.Priority = \'Medium\';
newTask.Type = \'Text Inbound\';
newTask.WhoId = sms.Contact__c;
newTask.ActivityDate = todaysDate;
newTask.Description = sms.smagicinteract__SMS_Text__c;
taskList.add(newTask);
}
insert taskList;
}
catch(Exception e){
}
}
Create Lead on Incoming SMS
trigger AutoLeadCreationOnIncomingSMS on smagicinteract__Incoming_SMS__c (before insert) {
try{
String mobileNo = \'\';
List leadList = new List();
for(smagicinteract__Incoming_SMS__c sms : Trigger.new){

mobileNo = sms.smagicinteract__Mobile_Number__c;
Lead lead = new Lead();
lead.MobilePhone = mobileNo;
lead.Status = \'Open - Not Contacted\';
lead.LastName = \'New Lead On Date : \'+system.now();
lead.Company = \'New Company On Date : \'+system.now();
lead.Description = sms.smagicinteract__SMS_Text__c;
leadList.add(lead);
}
insert leadList;
}
catch(Exception e){
}
}

Send SMS to Newly Assigned Task Owner

To send a SMS to a newly assigned task owner, you need to -

  1. Write Trigger on Task
  2. Get TaskIds
  3. Get New Task Owners using SOQL query
  4. Send SMS to New Task Owners

Find detailed code at following link.

/* @Trigger : SendSMSonOwnerChangeTrigger
@description : When a Task owner is changed, this Trigger sends SMS notification to newly assigned owner.
*/
trigger SendSMSonOwnerChangeTrigger on Task (after update){
List<smagicinteract__smsmagic__c>;  smsObjects = new List<smagicinteract__smsmagic__c>;();
String tplText = null;
Id leadId = null;
List taskList = Trigger.new;
Map<id, user> taskIdUserIdMap = new Map<id, user>();

for (Task newTask :taskList){

Task oldTask = System.Trigger.oldMap.get(newTask.Id);
if(oldTask.OwnerId != newTask.OwnerId){
taskIdUserIdMap.put(newTask.Id, newTask.OwnerId);
}
}
system.debug(Logginglevel.info, \' taskIdUserIdMap : \'+taskIdUserIdMap);
Map<id, user> userRecordMap = new Map<id, user>;
userRecordMap = ([select Id,Name,MobilePhone from User where id in :taskIdUserIdMap.values()]);
system.debug(Logginglevel.info, \' userRecordMap : \'+userRecordMap);

for (Task newTask :taskList){
Id taskOwnerId = taskIdUserIdMap.get(newTask.Id);
system.debug(Logginglevel.info, \' taskOwnerId : \'+taskOwnerId );
User user = userRecordMap.get(taskOwnerId);
system.debug(Logginglevel.info, \' user : \'+user);
if(user.MobilePhone != null){
tplText = newTask.Id+\'a Task has been assigned to you...\';
smagicinteract__smsMagic__c smsObj = new smagicinteract__smsMagic__c();
smsObj.smagicinteract__PhoneNumber__c = user.MobilePhone;
smsObj.smagicinteract__SMSText__c = tplText;
smsObj.smagicinteract__senderId__c = \'smsMagic\';
smsObj.smagicinteract__Name__c = user.Name;
smsObjects.add(smsObj);
}
}
insert smsObjects;
system.debug(Logginglevel.info, \' smsObjects : \'+smsObjects );
} // trigger code ends

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>