I have a simple scheduler class where I am trying to delete a set of records belonging to a particular queue. The class is working fine. But the test class is unable to recognize the delete statement as the list which is being produced is empty when running through test class. Below is the Class code:

global class caseDeletion Implements Schedulable{
global void execute(SchedulableContext sc){

public void deleteCases(){
        Group queueName = [SELECT Id,Name from Group where Name ='Delete Queue' and Type='Queue'];
        List<Case> caseDelList = [SELECT Id, caseNumber, Subject, Description FROM Case WHERE OwnerId = :queueName.Id];
            delete caseDelList;
    }catch(Exception e){
        system.debug('Error occured while deleting records'+e);

Below is the test class I am trying:

public class SCO_LubetekSpamCaseDeletionTest {
static testMethod void testLastComment(){
    Id caseRecTypeId = TestDataUtilities.getRecordTypeId('Case','My Record Type 1');        

    case caseRec = new case();

    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = false;
    dmo.assignmentRuleHeader.assignmentRuleID = null;

    caseRec.RecordTypeId = caseRecTypeId;
    caseRec.AccountId = TestDataUtilities.createAccount().Id;
    caseRec.Status = 'Open';

    Database.SaveResult sr = Database.insert(caseRec, dmo);
    if (sr.isSuccess()) {
        System.debug('Successfully inserted case. case ID: ' + sr.getId());

    Group queueName = [SELECT Id,Name from Group where Name ='Delete Queue' and Type='Queue'];
    Case caseRecordIns = [SELECT Id, OwnerId,caseNumber,owner.name FROM Case];
    caseRecordIns.OwnerId = queueName.Id;

    Database.update(caseRecordIns, dmo);
    System.debug('after update......'+caseRecordIns.OwnerId);

    /*caseRec.OwnerId = queueName.Id;
    update caseRec;
    System.debug('after update caseRec......'+caseRec.OwnerId);*/

    Case caseRecordIns1 = [SELECT Id, OwnerId,caseNumber,owner.name FROM Case];

    List<Case> caseDelList = [SELECT Id, caseNumber, Subject, Description,ownerid FROM Case];


    caseDeletion sh1 = new caseDeletion();
    String sch = '0 0 23 * * ?'; 
    system.schedule('Test caseDeletion', sch, sh1); 


Please let me know if anyone else has faced the same situation before and how to handle this.


Turns out I was trying to run the test class from dev console which was always running in synchronous mode which would always obey the Assignment Rules which are active. Once I ran it asynchronously from the test execution window via setup, there was no issue. Code works as it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.