I am encountering the below error when running my preliminary test code for my custom controller. I do plan to add some system.assert(); to the code, but would like to get the method to run first. The delete field exam method is failing.


System.ListException List Index Out of Bounds: 1

Controller showing the method:

public class fieldDynamicExtension {

    public List<FieldExam__c> delexamList               {get;set;}
    public List<FieldExam__c> examList                  {get;set;}
    public Loan_Memo__c myMemo                          {get;set;}
    public  LLC_BI__Loan__c myLoan                      {get;set;}
    public FieldExam__c myExam                          {get;set;}
    public Integer rowIndex                             {get;set;} 

    public fieldDynamicExtension() {
        //Set the fields via fieldset
        this.mymemo = new Loan_Memo__c();  
        //If the page is accessed from record, pull in the loan and relationship to related memo fields
        String currentRecordID = ApexPages.CurrentPage().getparameters().get('id');
        if (currentRecordID != null){
            myLoan  = [select id, LLC_BI__Account__c from LLC_BI__Loan__c where id =: currentRecordId];
            myMemo.Loan__c = myLoan.id;
            myMemo.Relationship__c = myLoan.LLC_BI__Account__c;}

        // Set the other memo field values and bind the field exam results to the id of the memo.

        myMemo.Name = 'Field Exam';
        myMemo.RecordTypeId = [Select Id From RecordType Where SObjectType = 'Loan_Memo__c' And DeveloperName='Field_Exam_Memo'].Id;
        myMemo.As_of__c = date.today();

        examList = new List<FieldExam__c>();
        Integer totalCount = examList.size();   
        delExamList  = new List<FieldExam__c>();} 

    public void addFieldExam(){
                examList.add(new FieldExam__c(LoanMemo__c = myMemo.Id));}

    public void deleteFieldExam(){
        String indexParam = ApexPages.currentPage().getParameters().get('rowIndex');
            if (String.isBlank(indexParam)) return;
            rowIndex = Integer.valueOf(indexParam); 
            myExam = examList.remove(rowIndex);

Test Class:

@isTest public class fieldDynamicExtensionTest {   
static void testFieldDynamicExtension(){

    //Create Loan, Relationship, Loan Memo, Field Exam
    Account myAcct = New Account (Name = 'TestAcct');
    insert myAcct;

    LLC_BI__Loan__c myLoan = New LLC_BI__Loan__c(Name = 'TestLoan', LLC_BI__Account__c=myAcct.id);
    insert myLoan;

    Loan_Memo__c myMemo = New Loan_Memo__c(Name = 'TestMemo', Loan__c = myLoan.id, Relationship__c = myLoan.LLC_BI__Account__c);
    insert myMemo;

    FieldExam__c myExam = New FieldExam__c(LoanMemo__c = myMemo.Id);
    insert myExam;

    //Instantiate VF Page
    PageReference pg = Page.FieldExam2;
    String currentRecordID = ApexPages.currentPage().getParameters().put('id', myLoan.id); 

     // Instantiate the controller run the methods

    fieldDynamicExtension ctrl = new fieldDynamicExtension();

    Integer rowIndex;
    String indexParam = ApexPages.currentPage().getParameters().put('rowIndex', '1');
    ctrl.deleteFieldExam(); }
  • Your code seems confused. You instantiate a new memo record, and then filter for child records that have its Id. But you haven't inserted it yet. What are you trying to accomplish here?
    – Adrian Larson
    Sep 15, 2016 at 14:08
  • 1
    Is this perhaps just an issue of trying to use 1-indexed access vs 0-indexed access? I only see you inserting a single FieldExam__c record in your test method. As Apex is 0-indexed, rowIndex would need to be set to 0 to delete the first entry in the list.
    – Derek F
    Sep 15, 2016 at 14:13
  • 1
    Beyond that, you should probably also include a check of rowIndex against examList.size() before attempting a deletion.
    – Derek F
    Sep 15, 2016 at 14:15
  • Thanks Derek and Adrian. Adrian I updated the code to set up a new list, rather than query for one. I had changed the code to create a new memo record rather than check if one existed. Sep 15, 2016 at 14:20
  • Derek setting the rowIndex to 0 worked. Thanks again! Sep 15, 2016 at 14:51


Your Answer

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