1

I have a few page redirects that do not want to work in my test class. The error I am running into is the partial URL reference.

ERROR: System.ListException: List index out of bounds: 0 Class.ACHBounceControllerTest.startAllOpenCases: line 9, column 1. pageReference pg = new PageReference('/' + openCases[0].Id);

I insert a value into the list so openCases[0].Id can't be empty. Am I syntactically writing it incorrectly?

@isTest
public with sharing class ACHBounceControllerTest {
  public static testmethod void nextCaseTest(){
    test.startTest();
    RecordType rt = new RecordType(Name = 'ACH Bounce');
    Case ca = new Case(Status='On Hold',Last_Stage_Change_Date__c = Datetime.now(), RecordType = rt, lastInArray__c = FALSE);
    insert ca;
    List<Case> openCases = [SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case WHERE (RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold') AND lastInArray__c = false) ORDER BY Last_Stage_Change_Date__c ASC];
    pageReference pg = new PageReference('/' + openCases[0].Id);
    Test.setCurrentPage(pg);
    ApexPages.StandardController sc = new ApexPages.standardController(ca);
    ACHBounceController controller = new ACHBounceController(sc);
    controller.nextCase();
    test.stopTest();
  }
}

APEX:

public PageReference nextCase() {
    if((openCases[0].Status == 'Sent to Accounting') || (openCases[0].Status == 'On Hold')) {
        system.debug('page: ' + '/' + openCases[0].Id);
        PageReference ReturnPage = new PageReference('/' + openCases[0].Id);
        ReturnPage.setRedirect(true);
        return ReturnPage;
    }
    return null;
}

UPDATES:

Attempted to update the RecordType correctly:

public static testmethod void nextCase(){
    test.startTest();
    //Did not work--> Id recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce').getRecordTypeId());
    RecordTypeInfo recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce');
    system.debug('rec type id: ' + recordTypId);
    Case ca = new Case(Status='On Hold',Last_Stage_Change_Date__c = Datetime.now(), RecordType = recordTypId, lastInArray__c = FALSE);
    insert ca;
    system.debug('test ca: ' + ca);

    List<Case> testopenCases = [SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case WHERE (RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold') AND lastInArray__c = false) ORDER BY Last_Stage_Change_Date__c ASC];
    system.debug('test testopenCases: ' + testopenCases);
    //pageReference pg = new PageReference('/' + openCases.Id);
    pageReference pg = new PageReference('/' + ca.Id);
    Test.setCurrentPage(pg);
    ApexPages.StandardController sc = new ApexPages.standardController(ca);
    ACHBounceController controller = new ACHBounceController(sc);
    controller.openCases = testopenCases;

    controller.nextCase();
    test.stopTest();
}

No success - I am able to see that the Case is inserted and there is a record type id but the list is null- see picture. enter image description here

UPDATE 2: What I have found is that the SOQL query is not able to see the inserted records because of this part of the query: (Status = 'Sent to Accounting' OR Status ='On Hold'). Status is a picklist field and I am unable to insert it's value.

public static testmethod void nextCaseTest(){
    test.startTest();
    Id recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce').getRecordTypeId();
    Case ca = new Case(Status='Sent to Accounting', RecordTypeid = recordTypId, lastInArray__c = True);
    insert ca;
    Case ca2 = new Case(Status='On Hold', RecordTypeid = recordTypId, lastInArray__c = True);
    insert ca2;
    Case ca3 = new Case(Status='Sent to Accounting', RecordTypeid = recordTypId, lastInArray__c = True);
    insert ca3;
    system.debug('ca: ' + ca);
    List<Case> openCases = [SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case WHERE RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold') AND lastInArray__c = TRUE ORDER BY Last_Stage_Change_Date__c ASC];
    system.debug('2openCases: ' + openCases);
    pageReference pg2 = new PageReference('/' + openCases[0].Id);
    system.debug('2openCases[0].Id: ' + openCases[0].Id);
    Test.setCurrentPage(pg2);
    ApexPages.StandardController sc = new ApexPages.standardController(ca);
    ACHBounceController controller = new ACHBounceController(sc);
    controller.openCases = openCases;
    controller.nextCase();
    test.stopTest();
}
  • 1
    openCases within ACHBounceController is not the same as openCases within your unit test. Are you sure the controller is querying the cases correctly? Perhaps more importantly, your nextCase method should probably not assume there is a next case. – sfdcfox Apr 17 '17 at 18:19
0

There are 2 issues.

  1. Getting the recordtypeid from name and assign that in Case record.

You have used RecordType rt = new RecordType(Name = 'ACH Bounce');

Now during creation of Case record, we are supposed to specify the RecordTypeId.

Here, due to that declaration, RecordTypeId will be null.

So, the case will be created without any recordtype.

And your SOQL query contains WHERE condition with RecordTypeName. Thats why openCases is empty and hence openCases[0].Id is throwing the error.

Better, retrieve the recordTypeId like this and use that during case creation.

Id recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce').getRecordTypeId());
  1. You are creating cases in test method. Though you have named that as openCases variable. This variable value must be assigned to controller's openCases variable so that controller can understand that openCases has values.

Updated Test Method

test.startTest();
//RecordType rt = new RecordType(Name = 'ACH Bounce');
Id recordTypId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('ACH Bounce').getRecordTypeId());

Case ca = new Case(Status='On Hold',Last_Stage_Change_Date__c = Datetime.now(), RecordTypeid = recordTypId, lastInArray__c = FALSE);
insert ca;

List<Case> openCases = [SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case 
WHERE (RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold') 
AND lastInArray__c = false) 
ORDER BY Last_Stage_Change_Date__c ASC];

pageReference pg = new PageReference('/' + openCases[0].Id);
Test.setCurrentPage(pg);
ApexPages.StandardController sc = new ApexPages.standardController(ca);
ACHBounceController controller = new ACHBounceController(sc);
controller.openCases = openCases; //assign the values to controller.

controller.nextCase();
test.stopTest();

Update, based on comments

You need to add @isTest(SeeAllData=false), then only the record you are inserting will be retrieved as long as query condition is satisfied.

  • the recordTypeId makes sense. I updated my code to reflect this change. I figured that the array was coming up as null because of one of the filters on the query. The issue with this update is that the query is still unable to read the record type. I am getting the same error message. I have mixed up a few different ways to write the recordTypId string. I will update my original question with these. – Olivia Apr 17 '17 at 20:20
  • I have already added the code using recordtypeId, you can use that – Santanu Boral Apr 17 '17 at 20:24
  • I copied what you wrote but I am still getting the same error. – Olivia Apr 17 '17 at 20:25
  • Can you check if this is working?, SELECT Id, Status, CaseNumber,Last_Stage_Change_Date__c FROM Case WHERE Id =:ca.id – Santanu Boral Apr 17 '17 at 20:33
  • seeAllData = false, you have to add – Santanu Boral Apr 17 '17 at 20:36
0

I found the solution. Status is a picklist for Case. I was directly inserting the status value when creating a mock case. The default value was always being set to 'Open' every time a Case was created. I assume this is a workflow rule my company create a while back that I was unaware of. If it is a Salesforce standard feature please let me know! Now, when I insert the Case first, and then update the Case's status, I am able to do a page reference to the index 0 of the list.

Case ca = new Case(Last_Stage_Change_Date__c = Datetime.now(), RecordTypeid = recordTypId, lastInArray__c = True);
insert ca;
ca.Status = 'On Hold';
update ca;
pageReference pg2 = new PageReference('/' + openCasestest[0].Id);

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.