1

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

  • The code producing the error is not posted above. The code throwing the error is OpportunityController.updateNextStep. You only posted the OpportunityController. insertRenewal. In your test method, you hard coded an ID (NEVER EVER DO THIS) and that record does not exist (Tests starts with no data) thus why you eventually end up with the Index out of bounds. See my answer, maybe you simply renamed the class method – Eric Jul 20 '15 at 1:37
1

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;

            }
        }
    }
}
1

The code producing the error is not posted above. The code throwing the error is OpportunityController.updateNextStep. You only posted the OpportunityController. insertRenewal (Maybe you renamed it??). In your test method, you hard coded an ID (NEVER EVER DO THIS) and that record does not exist (Tests starts with no data) thus why you eventually end up with the Index out of bounds.

So you will have to restructure you test like such:

@isTest
public class OpportunityTestClass {
    static testMethod void validateOpportunity(){
        Account acct = New Account(Name='Test Account');
        insert acct;
        date dateClose = date.parse('1/1/2016');

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

        insert opp;

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

    }

}
  • I did rename it, my apologies. So a better way of going about this would be to generate a new account rather than hard coding an account id?. Thanks for the feedback – shartshooter Jul 20 '15 at 4:58

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.