+ Start a Discussion

Count how many times email text has been used with schedule batch

Hey all,


So I have a requirement that my client wants to be able to count the number of times a canned email response is used. They are using quick texts for cases and they want to monitor how many times the message field is contained in the text body of an email, for every quick text record. They also want to break it up into three fields, how many emails contain the text in that last 30, 7, and one days. So I wrote up a scheduled class and a batch class to try and handle this and I wanted to get some feedback if I am approaching this the correct way. They also wants to run this every 24 hours. Code posted below.


Scheduled Class: 


global class emailCountScheduled implements Schedulable{

global void execute(SchedulableContext sc) {

List<QuickText> qt = new List<QuickText> ();
Map<String, QuickText> responseToQT = new Map<String, QuickText> ();
List<String> messages = new List<String> ();
Date dateFilter = Date.Today().addDays(-30);
String dateFilterString = String.valueOf(dateFilter);

qt = [SELECT Id, Message, Total_Use_1_Day__c, Total_Use_30_Days__c, Total_Use_7_Days__c FROM QuickText];

for(QuickText q : qt){
if(q.Message != null){
responseToQT.put(q.Message, q);

String query = 'SELECT Id, Subject, Description, ActivityDate FROM task WHERE Subject LIKE \'%Email%\' AND ActivityDate > dateFilter';
query = query.replace('dateFilter', dateFilterString);

emailCountBatch b = new emailCountBatch(responseToQT,messages,query);



Batch class:


global class emailCountBatch implements Database.Batchable<sObject> {

global final String query;
global final Map<String, QuickText> responseObjects;
global final List<String> cannedResponses;

global emailCountBatch(Map<String, QuickText> qt, List<String> messages, String q) {

responseObjects = new Map<String, QuickText> ();
cannedResponses = new List<String> ();

responseObjects = qt; cannedResponses = messages; query = q;

global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);

global void execute(Database.BatchableContext BC, List<sObject> scope) {
Map<String, List<sObject>> thirtyDays = new Map<String, List<sObject>> ();
Map<String, List<sObject>> sevenDays = new Map<String, List<sObject>> ();
Map<String, List<sObject>> oneDay = new Map<String, List<sObject>> ();

for(sObject s : scope){

String emailBody = String.ValueOf(s.get('Description'));

for(String m : cannedResponses){
thirtyDays.put(m, new List<sObject> ());

List<sObject> objectList = thirtyDays.get(m);
thirtyDays.put(m, objectList);

//Check if within 7 days
if(Date.ValueOF(s.get('ActivityDate')) > Date.Today().addDays(-7)){
sevenDays.put(m, new List<sObject> ());

List<sObject> objectList = sevenDays.get(m);
sevenDays.put(m, objectList);

//Check if within 1 day
if(Date.ValueOF(s.get('ActivityDate')) > Date.Today().addDays(-1)){
oneDay.put(m, new List<sObject> ());

List<sObject> objectList = oneDay.get(m);
oneDay.put(m, objectList);

for(String s : responseObjects.keySet()){
QuickText currentQT = responseObjects.get(s);
currentQT.Total_Use_30_Days__c += thirtyDays.get(s).size();
currentQT.Total_Use_7_Days__c += sevenDays.get(s).size();
currentQT.Total_Use_1_Day__c += oneDay.get(s).size();

global void finish(Database.BatchableContext BC) {
update responseObjects.values();


I appreciate any feedback. Thanks guys.