You need to sign in to do that
Don't have an account?
System.ListException: List index out of bounds: 0 - Please Help
Hello All,
I have a VF Page that displays a list of Opportunites when a custom field OS_Number__c contains the same value.
My scenario is...
User selects an Opportunity,
Then my controller JoinedProposalCheckController uses the Opportunity from the currentPage() to builds List OSNUM to contain that Opportunies OS_Number__c.
Then my controller JoinedProposalCheckController builds List OpptyList to contain all Opportunites that have the same OS_Number__c contained in List OSNum..
Finally my VF page JoinedProposalCheck displayes the results for the queries.
This works as expected.
I am unable to get my test class to work, please please help.
Test Class Error:
System.ListException: List index out of bounds: 0
Stack Trace:
Class.JoinedProposalCheckController.<init>: line 43, column 1
Class.JoinedProposalCheckController_Test.testEx: line 253, column 1
* I had to remove some code to load that loaded Oppotunity fields in order upload my Test Class.
Line 253 in my test class is:
JoinedProposalCheckController obj = new JoinedProposalCheckController(sc);
Here's my code:
Apex Controller:
public with sharing class JoinedProposalCheckController { public List<Opportunity> OpptyList{get;set;} public JoinedProposalCheckController(ApexPages.StandardController sc) { // Get Opportunity.OS_Number__c for passed Opportunity List<Opportunity> OSNum = [SELECT OS_Number__c FROM Opportunity //WHERE Id = '0060y000017j0IRAAY' WHERE Id = :ApexPages.currentPage().getParameters().get('id') LIMIT 1]; OpptyList = [SELECT Id, Name, Account.Name, OS_Number__c, Opportunity_Number__c, Joint_Proposal__c FROM Opportunity WHERE OS_Number__c = :OSNum[0].OS_Number__c LIMIT 10]; } }
Visualforce Page:
<apex:page standardController="Opportunity" extensions="JoinedProposalCheckController" showHeader="true" sidebar="false" tabStyle="Opportunity"> <apex:form id="JPForm"> <apex:pageBlock title="Opportunities List" > <apex:pageBlockSection > Please review Opportunities without a Joined Proposal </apex:pageBlockSection> <apex:pageBlockSection > <apex:pageBlockTable value="{!OpptyList}" var="op" width="100%"> <apex:column headerValue=" Opportunity"> <apex:outputLink value="/{!URLFOR(op.Id)}">{!op.Opportunity_Number__c} </apex:outputLink> </apex:column> <apex:column Value="{!op.Name}" headerValue="Opportunity Name___________________ "> </apex:column> <apex:column Value="{!op.OS_Number__c}" headerValue=" OS Number"> </apex:column> <apex:column Value="{!op.Account.Name}" headerValue="Account Name______________________________________ "> </apex:column> <apex:column Value="{!op.Joint_Proposal__c}" headerValue="Joined Proposal____________________________________________________________"> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Test Class:
@isTest public class JoinedProposalCheckController_Test { static testMethod void testEx() { // Add First Opportunity Opportunity opportunity_Obj1 = new Opportunity(AccountId = '0018000000f7GkqAAE', Name = 'First Opportunity', StageName = 'Qualified', CloseDate = Date.today(), CreatedDate = DateTime.now(), CreatedById = '00580000002GuxyAAC', LastModifiedDate = DateTime.now(), Joint_Proposal__c = 'a061A00001So9e9QAB', OS_Number__c = '44831', PS_OTHER_Processes__c = false); Insert opportunity_Obj1; System.Debug('* First Opportunity Added *'+opportunity_Obj1.Id); // Add Second Opportunity Opportunity opportunity_Obj2 = new Opportunity(AccountId = '0018000000f7GkqAAE', Name = 'Second Opportunity', StageName = 'Qualified', CloseDate = Date.today(), CreatedDate = DateTime.now(), CreatedById = '00580000002GuxyAAC', LastModifiedDate = DateTime.now(), Joint_Proposal__c = 'a061A00001So9e9QAB', OS_Number__c = '44831', PS_OTHER_Processes__c = false); Insert opportunity_Obj2; System.Debug('** Second Opportunity Added **'+opportunity_Obj2.Id); // Add Third Opportunity Opportunity opportunity_Obj3 = new Opportunity(AccountId = '0018000000f7GkqAAE', Name = 'Third Opportunity', StageName = 'Qualified', CloseDate = Date.today(), CreatedDate = DateTime.now(), CreatedById = '00580000002GuxyAAC', LastModifiedDate = DateTime.now(), Joint_Proposal__c = 'a061A00001So9e9QAB', OS_Number__c = '44831', PS_OTHER_Processes__c = false); Insert opportunity_Obj3; System.Debug('*** Third Opportunity Added ***'+opportunity_Obj3.Id); test.startTest(); List<Opportunity> OSNum = [SELECT OS_Number__c FROM Opportunity WHERE OS_Number__c = '44831' LIMIT 1]; List<Opportunity> OpptyList = [SELECT Id, Name, Account.Name, OS_Number__c, Opportunity_Number__c, Joint_Proposal__c FROM Opportunity WHERE OS_Number__c = '44831' LIMIT 10]; ApexPages.StandardController sc = new ApexPages.StandardController(opportunity_Obj1); JoinedProposalCheckController obj = new JoinedProposalCheckController(sc); test.stopTest(); } }
ApexPages.currentPage().getParameters().get('id')
Please use the code snippet below:
......................
PageReference myCurrentPage= Page.myVFPage; //replace myVFpage with your actual vf page name.
Test.setCurrentPage(myCurrentPage);
ApexPages.currentPage().getParameters().put('id',opportunity_Obj1.Id); // Put Id into the current page Parameters
ApexPages.StandardController sc = new ApexPages.StandardController(opportunity_Obj1);
JoinedProposalCheckController obj = new JoinedProposalCheckController(sc);
test.stopTest();
......
All Answers
Hi, the code is quite messy, are you getting the id parameter properly? Try to use a system.debug on it.
if not you could try with sc.getId();
If it helps please like and mark as correct, it may help others.
If not please tell me in which of the two queries of the constructor is failling.
good luck
ApexPages.currentPage().getParameters().get('id')
Please use the code snippet below:
......................
PageReference myCurrentPage= Page.myVFPage; //replace myVFpage with your actual vf page name.
Test.setCurrentPage(myCurrentPage);
ApexPages.currentPage().getParameters().put('id',opportunity_Obj1.Id); // Put Id into the current page Parameters
ApexPages.StandardController sc = new ApexPages.StandardController(opportunity_Obj1);
JoinedProposalCheckController obj = new JoinedProposalCheckController(sc);
test.stopTest();
......
@ David,
Thanks again for your patient assistance. If we ever meet I owe you rour beverage of choice.
Kind regards,
Robert