I'm looking to run a scheduled apex batch class that will create a task on any Contact record that had a sales rep assigned to them 1 day ago that hasn't been emailed yet.
I've created 2 custom fields to filter the criteria above.. the date the Contact was assigned a sales rep Sales_Rep_Updated__c
and a checkbox field that will be checked as soon as an Email task is completed, Welcome_Email_Sent__c
Here is my Schedulable Batch Class
global class Followup24Email Implements Schedulable, Database.Batchable<sObject>{
global void execute(SchedulableContext sc) {
Database.executeBatch(this);
}
//This is the query that is passed to the execute method. It queries all of the Contacts who have passed
String query = 'Select Id, AccountId, OwnerId, Sales_Rep_Updated__c FROM Contact WHERE Sales_Rep_Updated__c = YESTERDAY AND Welcome_Email_Sent__c = FALSE';
global database.queryLocator start(Database.BatchableContext BC) {
return database.getQueryLocator(query);
} //close start method
global void execute(Database.BatchableContext BC, list <Contact> scope) {
List <Task> taskList = new List<Task>();
// Iterate through the whole query of Contacts
// Create a Task that's associated with each Contact.
for(Contact c : scope) {
Task tsk = new Task();
tsk.OwnerId = c.OwnerId;
tsk.WhoId = c.Id;
tsk.WhatId = c.AccountId;
tsk.ActivityDate = System.today();
tsk.Status = 'Open';
tsk.Priority = 'Normal';
tsk.Subject = 'Send Welcome Email';
tsk.Description = 'This Account was recently assigned to you, please send them a welcome email.';
tsk.Type = 'Email';
tsk.IsReminderSet = true;
tsk.ReminderDateTime = System.now();
taskList.add(tsk);
} //close for-loop
try {
insert taskList;
} catch (system.dmlexception e) {
System.debug('Tasks not inserted: ' + e);
}
} //close execute method
global void finish(Database.BatchableContext BC) {
} //close finish method
}
And here is my Test class – currently getting 92% (24/26) code coverage
@isTest
private class Followup24EmailTest {
static testMethod void TestFollowup24Email() {
// User Id's for the two sales managers.
String aId = '00561000001DTdh';
List <Contact> contacts = new List <Contact>();
List <Task> tasks = new List <Task>();
// Create 50 Contacts
for (integer i=0; i<50; i++) {
Contact c = new Contact(FirstName='Test',
LastName='Contact'+ i,
Sales_Rep_Updated__c = Date.today().addDays(-1),
Welcome_Email_Sent__c = false,
OwnerId = aId);
contacts.add(c);
} //close for-loop
insert contacts;
Test.StartTest();
// Call the Batch Apex method.
Followup24Email fue = new Followup24Email();
ID batchprocessid = Database.executeBatch(fue);
Test.StopTest();
List<Task> taskResult = [SELECT ID From TASK];
System.debug('taskResult' + taskResult.size());
/*AsyncApexJob async = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems from AsyncApexJob where Id = :batchprocessid];
System.debug('Final results are ' + async);
System.AssertEquals(async.NumberOfErrors, 0);
System.AssertEquals([Select count() from Contact Where OwnerId=:aId AND FirstName='Test'], 50);
System.AssertEquals([Select count() from Task Where Subject = 'Send Welcome Email'], 50);*/
} //close testmethod
} //close Class
I've created a few test Accounts / Contacts with criteria that should fit (Welcome Email Sent = unchecked, and a Sales Rep Update date of yesterday) but no tasks are ever created.
Edit:
Here is what's not getting covered in my test
Any ideas? Thanks!
Best Answer
I would do something like this for your tests and work from there