[SalesForce] Test Class – System.ListException: List index out of bounds

I'm new to apex and am building a simple trigger that will create a new renewal opportunity when an opportunity is marked as Closed/Won.

In my dev account the trigger works flawlessly, however my test class is getting errors when I try to cover the code.

Opportunity Controller

public class OpportunityController {

    public static void insertRenewal(Opportunity[] opptys) {

        for (Opportunity opp :opptys){

            Opportunity oldOpp = (Opportunity)trigger.oldMap.get(opp.Id);

            if(opp.isWon && opp.StageName != oldOpp.StageName){

                Opportunity insertOpp = new Opportunity();
                insertOpp.Name = opp.Name + ' Renewal 1';
                insertOpp.StageName = 'Prospecting';
                List<Account> accts = [Select Id from Account where Id =: oldOpp.AccountId];

                insertOpp.AccountId = accts.get(0).id;

                date dateClose = date.parse('1/1/2016');
                insertOpp.CloseDate = dateClose;
                insertOpp.Amount = opp.Amount;
                insert insertOpp;

            }
        }
    }
}

Test Class

@isTest
public class OpportunityTestClass {
    static testMethod void validateOpportunity(){

        date dateClose = date.parse('1/1/2016');

        Opportunity opp = new Opportunity(
            Name='test opp', StageName = 'Negotiation/Review',
            CloseDate = dateClose,Amount = 10000);
        opp.AccountId = '00137000002h6VpAAI';

        insert opp;

        opp.StageName = 'Closed Won';
        update opp;

    }

}

The error I get is on line 16 which is insertOpp.AccountId = accts.get(0).id;

System.DmlException: Update failed. First exception on row 0 with id 00637000003a7HhAAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityTrigger: execution of AfterUpdate

caused by: System.ListException: List index out of bounds: 0

Class.OpportunityController.updateNextStep: line 16, column 1
Trigger.OpportunityTrigger: line 4, column 1: []

I recognize that the issue is that the query is not returning any accounts but I can't figure out why. I'm not new to programming but am new to apex/java so I'm not sure of the syntax or if I'm doing things in the wrong way.

Thanks in advance

Best Answer

OK, I figured out the solution to my own problem.

I updated the code below. Essentially, I removed the query and changed the code to reference opp. If anyone has a better solution I'm all ears.

public class OpportunityController {

    public static void insertRenewal(Opportunity[] opptys) {

        for (Opportunity opp :opptys){

            Opportunity oldOpp = (Opportunity)trigger.oldMap.get(opp.Id);

            if(opp.isWon && opp.StageName != oldOpp.StageName){

                Opportunity insertOpp = new Opportunity();
                insertOpp.Name = opp.Name + ' Renewal 1';
                insertOpp.StageName = 'Prospecting';
                insertOpp.AccountId = opp.AccountId;
                date dateClose = date.parse('1/1/2016');
                insertOpp.CloseDate = dateClose;
                insertOpp.Amount = opp.Amount;
                insert insertOpp;

            }
        }
    }
}
Related Topic