function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Max_gMax_g 

List index out of bounds: 0

I have a VF page that is using a datatable.  When I get to 50 rows, I am getting the error message.  How do I code for additional rows, or can I set the number of rows to be displayed on the page ? 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SamuelDeRyckeSamuelDeRycke
list < Sales_History_Comp__c > SHCID = [select id from sales_History_Comp__c where Account__c = : SHAID];
accounts[i].SHCOMP_ID__C = SHCID[0].id;

 Need I explain?

 

Besides this, I highly agree that you shouldn't do soql inside an iteration. Even if you consider it unlikly to hit a governer limit in your organisation, it is still terribly inefficient.

 

 

 

All Answers

S91084S91084

You limit the rows on the page by setting the "Rows" attribute to the <apex:datatable/>

 

or else use Standard Set Controller to set the page size in the apex class

 

Recent release is support OFFSET in the SOQL queries.

 

You can use either one of teh above options based on your requirement.

Max_gMax_g

Added rows = 100 to my datatale. 

Still got the error.

 

Error is on record 38

 

here is the code for my VF page and controller.

 

public with sharing class UsrAcct {

    public PageReference Cancel() {
        return null;
    }

Public   string newname = Apexpages.currentpage().getparameters().get('id2');

List<Account> Accounts;
public List<Account> getAccounts() {
string UserId=UserInfo.getUserId();
system.debug('User Id = ' + Userid);
Id id = ApexPages.currentPage().getParameters().get('id');

system.debug('Apex Id = ' + id);
system.debug('Apex NewName = ' + newname);
if(id == null) id = Userid;
if(Accounts== null) Accounts = [Select id,name, Current_YTD_Sales__c,Owner_Name__c,SHCOMP_ID__c from Account where OwnerId = :id and Current_YTD_Sales__c != 0 order by name];
if(Accounts.size() > 0) accounts[0].Owner_Name__c = newname;
for (Integer i=0; i<Accounts.size(); i++) {
                    ID SHAID = Accounts[i].id;            
                    list<Sales_History_Comp__c > SHCID = [select id from sales_History_Comp__c where Account__c = :SHAID];
                    accounts[i].SHCOMP_ID__C = SHCID[0].id;}
system.debug('Accounts to return = ' + Accounts);                                    
return Accounts;
  }
public pagereference slct()
{
String selectedTask = System.currentPageReference().getParameters().get('moveid');
String selectedName = System.currentPageReference().getParameters().get('SLCTNAME');
String selectedSHC = System.currentPageReference().getParameters().get('SHCID');

system.debug('Selected name = ' + selectedName);
system.debug('Selected id = ' + selectedTask);
system.debug('Selected SHCid = ' + selectedSHC);

String uid=ApexPages.currentPage().getParameters().get('id');
system.debug('UID = ' + uid);

PageReference newURL=new PageReference('/apex/Sales_History_Comparison');
newURL.getParameters().put('id',selectedSHC);
newURL.getParameters().put('id2',selectedName);
newURL.setRedirect(true);
system.debug('NEWURL = ' + NewURL);
return newURL;
}
public pagereference redirectfun()
{
string str=userinfo.getuserid();
PageReference newURL=new PageReference('/apex/Manager_Inquiry');
newURL.getParameters().put('id',str);
newURL.setRedirect(true);
return newURL;
}

  }

 

VF Page

<apex:page Controller="UsrAcct" id="MyAccounts">

<!--     Created by: Max Gilbert     Last Update: June 29 2012 SFDC     -->         <apex:pageBlock title="List of Accounts for  {!$User.FirstName} {!$User.LastName}  ">     </apex:pageBlock>   <apex:pageBlock title="My Accounts with balances this year  ">    <apex:form >                <apex:dataTable value="{!Accounts}" var="Accts" id="MyAccounts" cellPadding="4" border="1" rows="100">    <apex:column >             <apex:facet name="header">Action</apex:facet>             <apex:commandLink action="{!slct}" value="Select" style="fontWeight:bolder" id="ReplyLink" onclick="" >             <apex:param name="moveid" value="{!Accts.Id}"/>             <apex:param name="SLCTNAME" value="{!Accts.name}"/>             <apex:param name="SHCID" value="{!Accts.SHCOMP_ID__c}"/>                          </apex:commandLink>   </apex:column>      <apex:column headervalue="Account Name"> <apex:outputText value="{!Accts.name}"/> </apex:column>           <apex:column headervalue="YTD Sales"> <!-- <apex:outputText value="{!Accts.Current_YTD_Sales__c}"/> --> <apex:outputText style="float:right" value="{0, number, ,000}">

    <apex:param value="{!Accts.Current_YTD_Sales__c}" /> </apex:outputText>

</apex:column>                                                   </apex:dataTable>     <apex:commandButton value="Return" action="{!Cancel}"/>            </apex:form> </apex:pageBlock>                              </apex:page>

S91084S91084

What exactly are you trying to achieve in the getAccounts method. It is not best pratcise to write the queries inside for loop.

Can you explain your object model and wht you are trying to achieve.

Max_gMax_g
This is a drill down based on role hierarchy. This particular VF page lists the accounts that a sales rep owns and then allows them to select an account and view a 2 year comparison of their sales dollars. No sales rep will ever have more than 100 active accounts. Accounts with zero sales dollars for the year are not considered active. My problem is when a sales rep has more than 37 active customers I am getting the error and that keeps me from being able to display any of their accounts. Max Gilbert IT Infrastructure and Operations National Envelope 3211 Internet Blvd. Suite200 Frisco, TX 75034 Tel: 972-731-2788 Fax: 972-731-1156 National Envelope...Where our customers come first
SamuelDeRyckeSamuelDeRycke
list < Sales_History_Comp__c > SHCID = [select id from sales_History_Comp__c where Account__c = : SHAID];
accounts[i].SHCOMP_ID__C = SHCID[0].id;

 Need I explain?

 

Besides this, I highly agree that you shouldn't do soql inside an iteration. Even if you consider it unlikly to hit a governer limit in your organisation, it is still terribly inefficient.

 

 

 

This was selected as the best answer