Schedule SMS

July 03, 2014

How to Schedule SMS

Scheduler is salesforce’s feature using which we can schedule the job for particular day and time. We can schedule apex class which implements interface called as Schedulable.

The schedulable interface contains one method that must be implemented, the method is “execute”

global void execute(SchedulableContext sc){}

In this method you can write your business logic. Once you done with the business logic just follow the steps given below and schedule the apex class.

Goto to Setup –> App Setup –> Develop –> Apex classes.
apex-class

I am text block. Click edit button to change this text.

Click on “Schedule Apex” button and fill all the required fields and save it.
Schedule-apex

According to above example (above screen shot) class is ready to execute. This class will run everyday and send SMS according to the business logic implemented.

Types of Scheduling
Prepare SMS at time of schedule

When you want to schedule message for group of numbers. Then you should create the instance of object smagicinteract__Scheduled_SMS__c and attach the required fields such as smagicinteract__MobilePhone__c, smagicinteract__status__c, smagicinteract__Scheduled_Date__c, smagicinteract__SMSText__c. Schedule records will be picked at time of sending and sent out.

(“smagicinteract”is package prefix & differs from package to package )

The advantage of this type is that you can cancel the schedule by deleting the records.The following sample code explain it in more detail. Suppose you want to schedule SMS on Contact.

 List< Contact > conList = [select Id,FirstName, LastName, MobilePhone, Name from
Contact];
Date scheduleDate = Date.valueOf('2011-08-10');
List<smagicinteract__Scheduled_SMS__c> scheduleSMSList = new
List<smagicinteract__Scheduled_SMS__c>();
if(conList != null){
for(Contact contact : conList){
smagicinteract__Scheduled_SMS__c scheduleSMSObject = new
smagicinteract__Scheduled_SMS__c();
if(contact.MobilePhone != null){
scheduleSMSObject.smagicinteract__MobilePhone__c = contact.MobilePhone;
scheduleSMSObject.smagicinteract__jobId__c = '1';
scheduleSMSObject.smagicinteract__status__c = 'Schedule';
scheduleSMSObject.smagicinteract__Scheduled_Date__c = scheduleDate;
scheduleSMSObject.smagicinteract__SMSText__c = 'Test Of Schedule';
}
scheduleSMSList.add(scheduleSMSObject);
}
insert scheduleSMSList;
}

After this you write a schedulable class which just pickup scheduled_SMS__c object’s record according to scheduleDate and send SMS by creating SMS_Magic__c Object or SMSPushAPI. Click Here to see how to send sms using Apex.

Message rendered at schedule execution.
Here you just schedule a sms on particular object. We do not create the object of smagicinteract__Scheduled_SMS__c, we just schedule the SMS on a particular object. We can write one Schedulable class that will pick up a particular object records according to their timestamps. Render SMS drafts using Template created for that object and sms is sent.To cancel scheduled sms,  you need to delete the scheduled job itself. The following sample code is to schedule SMS from a custom object “Time sheet”.
global class TodaysScheduleSMS implements Schedulable {
String day = '';
String query = '';
List<Time_Sheet_Schedule__c> todaysScheduleList = null;
String smsText = '';
String status = '';
String week = '';
global void execute(SchedulableContext sc){
// Here we can get today's day.
day = DateTime.now().format('EEEEE');
todaysScheduleList = [select SMS_Template__c, Status__c, Day__c, Week__c from
Time_Sheet_Schedule__c where Day__c =:day ];
if(todaysScheduleList.size() > 0){
for(Time_Sheet_Schedule__c timeSheetScheduleObj : todaysScheduleList){
status = timeSheetScheduleObj.Status__c;
week = timeSheetScheduleObj.Week__c;
List<smagicinteract__SMS_Template__c> smsTemplateList = [select
smagicinteract__Text__c from smagicinteract__SMS_Template__c where Id
=:timeSheetScheduleObj.SMS_Template__c];
if(smsTemplateList != null){
smsText = smsTemplateList[0].smagicinteract__Text__c;
}
}
/* write here code to send SMS to number */
}
}
}

There are Governor limits for how many callouts and DML statements, you can execute from Scheduled job. It will limit you to send 10-20 sms in one job. We found workaround of this limit, you can use batchable class described below, to send  bulk SMS from scheduled job.

global class SendScheduleSMSBatch implements Database.Batchable<sObject>,
Database.AllowsCallouts {

global SendScheduleSMSBatch(){
}
global Database.QueryLocator start(Database.BatchableContext BC){
String day = DateTime.now().format('EEEEE');
String scheduleStatus = 'Schedule';
String query = 'select SMS_Template__c, Status__c, Day__c, Week__c from
Time_Sheet_Schedule__c where Day__c =:day';
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){
if(scope.size() > 0){
for(Time_Sheet_Schedule__c timeSheetScheduleObj : todaysScheduleList){
status = timeSheetScheduleObj.Status__c;
week = timeSheetScheduleObj.Week__c;
List<smagicinteract__SMS_Template__c> smsTemplateList = [select
smagicinteract__Text__c from smagicinteract__SMS_Template__c where Id
=:timeSheetScheduleObj.SMS_Template__c];
if(smsTemplateList != null){
smsText = smsTemplateList[0].smagicinteract__Text__c;
}
}
/* write here code to send SMS to number */
}
global void finish(Database.BatchableContext BC){
// send batch execution email;
}
}

Here from Schedulable class you need to call batch class as described below.

SendScheduleSMSBatch batchSMS = new SendScheduleSMSBatch();

Database.executeBatch(batchSMS);

Test class for this batch class is given below.

@isTest
private class SendScheduleSMSBatchTest {

static Time_Sheet_Schedule__c timeSheetSchedule = null;
static smagicinteract__SMS_Template__c smsTpl= null;
static void setupTest(){

smsTpl = new smagicinteract__SMS_Template__c();
smsTpl.smagicinteract__Text__c = 'Test of Schedule SMS';
smsTpl.smagicinteract__Name__c = 'Schedule SMS Template'
smsTpl.smagicinteract__ObjectName__c = 'Time_Sheet_Schedule__c';
insert smsTpl;
timeSheetSchedule = new Time_Sheet_Schedule__c();
timeSheetSchedule.SMS_Template__c= smsTpl.Id;
timeSheetSchedule.Status__c = 'Scheduled';
timeSheetSchedule.Day__c = 'Monday';
timeSheetSchedule.Week__c = 'this';
insert timeSheetSchedule;
}
static void testMethod test_ScheduleSMSBatch(){
Test.StartTest();
setupTest();
SendScheduleSMSBatch sscb = new SendScheduleSMSBatch();
ID batchprocessid = Database.executeBatch(sscb);
Test.stopTest();
List<Time_Sheet_Schedule__c> timeSheetScheduleList = [select Id, Status__c
from Time_Sheet_Schedule__c where Id =:timeSheetSchedule.Id];
system.assertEquals(timeSheetScheduleList[0].Status__c, 'Sent');
}
}

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>