{"id":1404,"date":"2020-07-24T11:13:12","date_gmt":"2020-07-24T11:13:12","guid":{"rendered":"https:\/\/staging.txtbox.in\/docs\/developers\/?post_type=lsvr_kba&#038;p=1404"},"modified":"2020-07-24T11:13:19","modified_gmt":"2020-07-24T11:13:19","slug":"schedule-sms-messages","status":"publish","type":"lsvr_kba","link":"https:\/\/staging.txtbox.in\/docs\/developers\/knowledge-base\/schedule-sms-messages\/","title":{"rendered":"Schedule SMS Messages"},"content":{"rendered":"\n<span>With the Salesforce.com scheduler, you can schedule a SMS job for a particular day and time. We can accomplish this by employing the <b>Schedulable<\/b> Apex interface.<\/span>\n<br>\n<span>The <b>Schedulable<\/b> interface contains one method that must be implemented, the method is <b>Execute<\/b>.<\/span>\n<br>\n<pre>global void execute(SchedulableContext sc){}<\/pre>\n<br>\n<span>In this method you can write your business logic, and then follow the steps given below and schedule the Apex class.<\/span>\n<br>\n<span><b>Step 1 \u2013 Goto to Setup \u2013&gt; App Setup \u2013&gt; Develop \u2013&gt; Apex classes.<\/b><\/span>\n<br>\n<img decoding=\"async\" src=\"https:\/\/www.sms-magic.com\/docs\/developers\/wp-content\/uploads\/sites\/10\/2019\/11\/sms_magic_73.png\">\n<br>\n<hr class=\"wp-block-separator is-style-wide\">\n<span><b>Step 2 \u2013 Click the Schedule Apex button, complete the form, and save<\/b><\/span>\n<br>\n<span>In the figure above, we can see that the class is ready to execute. This class is set to run each day and send SMS messages according to the business logic.<\/span>\n<br>\n<img decoding=\"async\" src=\"https:\/\/www.sms-magic.com\/docs\/developers\/wp-content\/uploads\/sites\/10\/2019\/11\/sms_magic_73.png\">\n<br>\n<hr class=\"wp-block-separator is-style-wide\">\n<span><b>Step 3 \u2013 Choose the type of scheduling<\/b><\/span>\n<br>\n<span><b>Prepare SMS at time of schedule<\/b><\/span>\n<br>\nTo schedule an SMS message to be sent to a group of numbers, first create the instance of <b>smagicinteract__Scheduled_SMS__c<\/b> object and attach the required fields such as <b>smagicinteract__MobilePhone__c<\/b>, <b>smagicinteract__status__c<\/b>, <b>smagicinteract__Scheduled_Date__c<\/b>, <b>smagicinteract__SMSText__c<\/b>. Any records that are scheduled will be picked at the time of distribution. Note that smagicinteract above is a package prefix which will differ from package to package.\n <br>\n<span>The advantage of this type is that you can cancel the schedule by simply deleting the records. The following sample code demonstrates this more clearly.<\/span>\n<br>\n<pre>List conList = [select Id,FirstName, LastName, MobilePhone, Name from\nContact];\nDate scheduleDate = Date.valueOf('2011-08-10');\nList scheduleSMSList = new\nList();\nif(conList != null){\nfor(Contact contact : conList){\nsmagicinteract__Scheduled_SMS__c scheduleSMSObject = new\nsmagicinteract__Scheduled_SMS__c();\nif(contact.MobilePhone != null){\nscheduleSMSObject.smagicinteract__MobilePhone__c = contact.MobilePhone;\nscheduleSMSObject.smagicinteract__jobId__c = '1';\nscheduleSMSObject.smagicinteract__status__c = 'Schedule';\nscheduleSMSObject.smagicinteract__Scheduled_Date__c = scheduleDate;\nscheduleSMSObject.smagicinteract__SMSText__c = 'Test Of Schedule';\n}\nscheduleSMSList.add(scheduleSMSObject);\n}\ninsert scheduleSMSList;\n}\n<\/pre>\n<br>\n<span>After this, use a <b>Schedulable<\/b> class to pickup the <b>scheduled_SMS__c<\/b> object record\u2013according to <b>scheduleDate<\/b> and send the SMS messages by creating the <b>SMS_Magic__c<\/b> object\u2013or us <b>SMSPushAPI<\/b>.<\/span>\n<br>\n<span><b>Message rendered at schedule execution<\/b><\/span>\n<br>\n<span>With this approach, you simply schedule on a particular object. We do not create the object of <b>smagicinteract__Scheduled_SMS__c<\/b>, but only schedule on a particular object. You can write one Schedulable class that will pick up particular object records according to the timestamps of each. Render SMS drafts using the template created for that object and the SMS messages will be sent. To cancel a scheduled SMS distribution,  you&#8217;ll need to delete the scheduled job itself. The following sample code will schedule SMS messages from a custom object named <b>Time sheet<\/b>.<\/span>\n<br>\n<pre>global class TodaysScheduleSMS implements Schedulable {\nString day = '';\nString query = '';\nList todaysScheduleList = null;\nString smsText = '';\nString status = '';\nString week = '';\nglobal void execute(SchedulableContext sc){\n\/\/ Here we can get today's day.\nday = DateTime.now().format('EEEEE');\ntodaysScheduleList = [select SMS_Template__c, Status__c, Day__c, Week__c from\nTime_Sheet_Schedule__c where Day__c =:day ];\nif(todaysScheduleList.size() &gt; 0){\nfor(Time_Sheet_Schedule__c timeSheetScheduleObj : todaysScheduleList){\nstatus = timeSheetScheduleObj.Status__c;\nweek = timeSheetScheduleObj.Week__c;\nList smsTemplateList = [select\nsmagicinteract__Text__c from smagicinteract__SMS_Template__c where Id\n=:timeSheetScheduleObj.SMS_Template__c];\nif(smsTemplateList != null){\nsmsText = smsTemplateList[0].smagicinteract__Text__c;\n}\n}\n\/* write here code to send SMS to number *\/\n}\n}\n}\n<\/pre>\n<br>\n<span>There are limits on how many callouts and DML statements you can execute from a scheduled job. Only 10-20 SMS messages can be sent in one job. There is a workaround to exceed this limit, in which you can use a batchable class \u2013 as outlined below.<\/span>\n<br>\n<pre>global class SendScheduleSMSBatch implements Database.Batchable,\nDatabase.AllowsCallouts {\n \nglobal SendScheduleSMSBatch(){\n}\nglobal Database.QueryLocator start(Database.BatchableContext BC){\nString day = DateTime.now().format('EEEEE');\nString scheduleStatus = 'Schedule';\nString query = 'select SMS_Template__c, Status__c, Day__c, Week__c from\nTime_Sheet_Schedule__c where Day__c =:day';\nreturn Database.getQueryLocator(query);\n}\n \nglobal void execute(Database.BatchableContext BC, List scope){\nif(scope.size() &gt; 0){\nfor(Time_Sheet_Schedule__c timeSheetScheduleObj : todaysScheduleList){\nstatus = timeSheetScheduleObj.Status__c;\nweek = timeSheetScheduleObj.Week__c;\nList smsTemplateList = [select\nsmagicinteract__Text__c from smagicinteract__SMS_Template__c where Id\n=:timeSheetScheduleObj.SMS_Template__c];\nif(smsTemplateList != null){\nsmsText = smsTemplateList[0].smagicinteract__Text__c;\n}\n}\n\/* write here code to send SMS to number *\/\n}\nglobal void finish(Database.BatchableContext BC){\n\/\/ send batch execution email;\n}\n}\n<\/pre>\n<br>\n<span>From the Schedulable class, you need to call a batch class:<\/span>\n<br>\n<pre>SendScheduleSMSBatch batchSMS = new SendScheduleSMSBatch();\nDatabase.executeBatch(batchSMS);\n<\/pre>\n<br>\n<span>An example test class for this batch class is given below.<\/span>\n<br>\n<pre>@isTest\nprivate class SendScheduleSMSBatchTest {\n \nstatic Time_Sheet_Schedule__c timeSheetSchedule = null;\nstatic smagicinteract__SMS_Template__c smsTpl= null;\nstatic void setupTest(){\n \nsmsTpl = new smagicinteract__SMS_Template__c();\nsmsTpl.smagicinteract__Text__c = 'Test of Schedule SMS';\nsmsTpl.smagicinteract__Name__c = 'Schedule SMS Template'\nsmsTpl.smagicinteract__ObjectName__c = 'Time_Sheet_Schedule__c';\ninsert smsTpl;\ntimeSheetSchedule = new Time_Sheet_Schedule__c();\ntimeSheetSchedule.SMS_Template__c= smsTpl.Id;\ntimeSheetSchedule.Status__c = 'Scheduled';\ntimeSheetSchedule.Day__c = 'Monday';\ntimeSheetSchedule.Week__c = 'this';\ninsert timeSheetSchedule;\n}\nstatic void testMethod test_ScheduleSMSBatch(){\nTest.StartTest();\nsetupTest();\nSendScheduleSMSBatch sscb = new SendScheduleSMSBatch();\nID batchprocessid = Database.executeBatch(sscb);\nTest.stopTest();\nList timeSheetScheduleList = [select Id, Status__c\nfrom Time_Sheet_Schedule__c where Id =:timeSheetSchedule.Id];\nsystem.assertEquals(timeSheetScheduleList[0].Status__c, 'Sent');\n}\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>With the Salesforce.com scheduler, you can schedule a SMS job for a particular day and time. We can accomplish this by employing the Schedulable Apex interface. 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, and then [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"lsvr_kba_cat":[153],"lsvr_kba_tag":[],"class_list":["post-1404","lsvr_kba","type-lsvr_kba","status-publish","format-standard","hentry","lsvr_kba_cat-sms-magic-for-developers"],"_links":{"self":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba\/1404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba"}],"about":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/types\/lsvr_kba"}],"author":[{"embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/comments?post=1404"}],"version-history":[{"count":1,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba\/1404\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba\/1404\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/media?parent=1404"}],"wp:term":[{"taxonomy":"lsvr_kba_cat","embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba_cat?post=1404"},{"taxonomy":"lsvr_kba_tag","embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba_tag?post=1404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}