My apex class is

tempinvoiceid = apexpages.currentpage().getparameters().get('tempinvoiceid');
 tempin = [select name,Invoice_contact__c,First_Name__c,State__c,Invoice_policy__c,Invoice_Check__c,Last_Name__c,country__c,Email__c from Invoice__c where id=:tempInvoiceid];
 paypolicy = [select id,Early_Days__c,early_Payment__c, invoice_policy__c from payment_policy__c where early_payment__c = true and invoice_policy__c = :tempin[0].Invoice_policy__c]; //getting error here

My test class is

    public static testmethod void testsitepage(){
     Invoice_Policy__c newpolicy = new Invoice_policy__c(name='testPolicy',net__c='3',policy_status__c='Active');
        insert newpolicy;

        payment_policy__c newpay = new payment_policy__c(name='testEarlyPayment',early_payment__c = true,Early_Days__c='2',Invoice_Policy__c = newpolicy.id);
        insert newpay;

       Invoice__c newIn = new Invoice__c(First_Name__c = 'test',state__c='Hyderabad', Invoice_policy__c=newpolicy.id);
        insert newIn;

        sitepage sp = new sitepage();

Invoice__c -------- Invoice_policy__c(lookup) payment_policy__c --------- Invoice_policy__c(lookup)


  • in the class the field on payment_policy__c is invoice_policy__c but in the test it's Invoice_Polici__c. Typo or you really have 2 similar fields? – eyescream Jan 17 '13 at 8:03
  • its a typo.Only 1 field – Eagerin Sf Jan 17 '13 at 8:05

That error means that you are trying to access an index in a list that doesn't exist. In this case that index is 0. The list access that you do on the line that is causing the error is tempin[0].Invoice_policy__c.

Your Apex class begins by getting an Id parameter, however your test class never sets that parameter in any of the code that you posted:

Apex class:

tempinvoiceid = apexpages.currentpage().getparameters().get('tempinvoiceid');

Test class:


Short answer: You put into the Map with a key of id, but get from the Map with a key of tempinvoiceid.

Longer answer: The getParameters() method returns a Map. When you call .put('id',newpolicy.id) in your test class you are putting a Map entry with a key of id into the Map. In your Apex class when you call .get('tempinvoiceid') you are attempting to get a Map value with a key of tempinvoiceid. Since you never put a Map entry with a key of tempinvoiceid into the Map null is returned as per the Map.get specification. On the next line your where id=:tempInvoiceid evaluates to where id = null which results in an empty (i.e., size 0) List being assigned to tempin. This causes the next line tempin[0] to throw a List Index Out of Bounds error, because there is no 0th element in an empty List.

I suggest making sure that any parameters that you use in your Apex class are set in your test class prior to using them in the Apex class.

Take a look at The Visualforce Developer's Guide, specifically the section on Testing Custom Controllers and Extensions for an example and more information. The example in the Visualforce Developer's Guide is explained more in a section of the An Introduction to Apex Code Test Methods developer force page.

  • 2
    I didn't know it works just to put ApexPages.currentPage()... ;) Still if he's learning - I'd advise to teach using Test.setCurrentPage() instead ;) salesforce.com/us/developer/docs/apexcode/Content/… – eyescream Jan 17 '13 at 8:10
  • As a Newbie to Test class Can you please explain somewhat more clearly if possible with an example. – Eagerin Sf Jan 17 '13 at 8:25

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.