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
DodiDodi 

VisualForce controller problem - Index out of bounds

 I am having a small issue with a controller I am trying to build. I was hoping I can run the issue by the board to see if anyone had any insight to the issue.

I have a VisualForce page that loads some records into a sortable table. The sort functionality s provided by a class called superSort(from a SFDC example posting located at http://wiki.developerforce.com/index.php/Sorting_Tables). I am able to successfully use it for the first time the page loads without a problem. Also on the page I have some fields that the user can set to pass parameters to the same query. I am running into issues with calling the supersort class a second time from my controller via a command button. There is an issue with my list and I am getting "Index out of bounds" errors when calling the super sort method. So the method works fine for my first list when the page is loded, just not the second one when I send in another request with the new parameters for the query and want the page refresed. I get the errors when I try to set the initial List to null.

Pasting my controller and VF code below.


Thanks

Dodi

Controller :

public class SalesAndForecastSort {

    public List<Sales_Forecasts__c> sf;
    public String sortField {get; set;}
    public String previousSortField {get; set;}
    public String sfRecordType {get; set;}
    public Id sfAccount {get; set;}
    public Id rt {get; set;}
    public Id rtInvoicesSales {get; set;}
    public Id rtOpenOrders {get; set;}
    public Id rtEvergreen {get; set;}
    public Id Salesforecast {get; set;}
    public String selectedRecordType {get; set;}
    public String selectedAccountingMonth {get; set;}
   
    public String accountingMonth {get; set;}
   
    
    public String getSelectedAccountingMonth() {
                return selectedAccountingMonth;
       }
   
    
    public String getSelectedRecordType() {
                return selectedRecordType;
        }
   /*
    public void setSelectedRecordType(Id selectedRecordType) {
                this.selectedRecordType = selectedRecordType;
        }
  


    public void setSelectedAccountingMonth(String selectedAccountingMonth) {
                this.selectedAccountingMonth = selectedAccountingMonth;
        }
       */
 
      
    public List<SelectOption> getRtOptions() {
            List<SelectOption> rtOptions = new List<SelectOption>();
           
            rtOptions.add(new SelectOption('NA','--'));
            rtOptions.add(new SelectOption('Invoiced','Invoiced'));
            rtOptions.add(new SelectOption('Open','Open'));
            rtOptions.add(new SelectOption('Evergreen','Evergreen'));
            rtOptions.add(new SelectOption('Salesforecast','Salesforecast'));
            return rtOptions;
           
        }
       
        public List<SelectOption> getAccountingMonths() {
            List<SelectOption> accountingMonths = new List<SelectOption>();
           
            accountingMonths.add(new SelectOption('NA','--'));
            accountingMonths.add(new SelectOption('January','January'));
            accountingMonths.add(new SelectOption('February','February'));
            accountingMonths.add(new SelectOption('March','March'));
            accountingMonths.add(new SelectOption('April','April'));
            accountingMonths.add(new SelectOption('May','May'));
            accountingMonths.add(new SelectOption('June','June'));
            accountingMonths.add(new SelectOption('July','July'));
            accountingMonths.add(new SelectOption('August','August'));
            accountingMonths.add(new SelectOption('September','September'));
            accountingMonths.add(new SelectOption('October','October'));
            accountingMonths.add(new SelectOption('November','November'));
            accountingMonths.add(new SelectOption('December','December'));
            return accountingMonths;
           
        }
       
        public List<Sales_Forecasts__c> getSF() {
       
         //get params from URL string      
         sfRecordType = ApexPages.currentPage().getParameters().get('sfRecordType');
         sfAccount = ApexPages.currentPage().getParameters().get('sfAccount');
         //Set up maps to hold the Record Type IDs and the literal values associated            
         Map<String, Id> sfRecordTypeMap = new Map <String,Id>();  
         //insert record ids into map
         for (RecordType asfRecordType : [SELECT Id, Name FROM RecordType WHERE IsActive = TRUE AND SobjectType = 'Sales_Forecasts__c' LIMIT 1000] ) {
                 sfRecordTypeMap.put(asfRecordType.Name, asfRecordType.Id);      
             }  
                                              
         //associate record type values to appropriate record types to be used in query
       
        if (sfRecordType.equals('Invoiced')) {
                rt = sfRecordTypeMap.get('Invoiced Sales');    
        }    
       
        if (sfRecordType.equals('Open')) {
                rt = sfRecordTypeMap.get('Open Orders');       
        }
       
        if (sfRecordType.equals('Evergreen')) {
                rtInvoicesSales = sfRecordTypeMap.get('Evergreen');    
        }
               
        if (sfRecordType.equals('SalesForecast')) {
                rtInvoicesSales = sfRecordTypeMap.get('Sales Forecast');       
        }
       
                if(sf == null){
                    sf = [Select Product2__c, Net_Proceeds_USD__c, Net_Proceeds_LC__c, Net_Proceeds_FC__c, Account__c From Sales_Forecasts__c Where Account__c = :sfAccount AND RecordTypeId = :rt];
                }
                return sf;
              
    }
 
 
//public List<Sales_Forecasts__c> getUserDefinedSF(String accountingMonth, String sfRecordType) {
       
       
         public List<Sales_Forecasts__c> getUserDefinedSF(String sfRecordType) {
       
       
            
              //sf.clear();
       
         //Set up maps to hold the Record Type IDs and the literal values associated            
         Map<String, Id> sfRecordTypeMap = new Map <String,Id>();  
         //insert record ids into map
         for (RecordType asfRecordType : [SELECT Id, Name FROM RecordType WHERE IsActive = TRUE AND SobjectType = 'Sales_Forecasts__c' LIMIT 1000] ) {
                 sfRecordTypeMap.put(asfRecordType.Name, asfRecordType.Id);      
             }  
                                              
         //associate record type values to appropriate record types to be used in query
       
        if (sfRecordType.equals('Invoiced')) {
                rt = sfRecordTypeMap.get('Invoiced Sales');    
        }    
       
        if (sfRecordType.equals('Open')) {
                rt = sfRecordTypeMap.get('Open Orders');       
        }
       
        if (sfRecordType.equals('Evergreen')) {
                rtInvoicesSales = sfRecordTypeMap.get('Evergreen');    
        }
               
        if (sfRecordType.equals('SalesForecast')) {
                rtInvoicesSales = sfRecordTypeMap.get('Sales Forecast');       
        }
                ///issue is here, value has to be null...but when clearing the list...out of bound exceptions occur
                if(sf == null){
               
                //sf = [Select Product2__c, Net_Proceeds_USD__c, Net_Proceeds_LC__c, Net_Proceeds_FC__c, Account__c From Sales_Forecasts__c Where Account__c = :sfAccount AND RecordTypeId = :rt];
                //sf = [Select Product2__c, Net_Proceeds_USD__c, Net_Proceeds_LC__c, Net_Proceeds_FC__c, Account__c From Sales_Forecasts__c Where RecordTypeId = :rt];
                sf = [Select Product2__c, Net_Proceeds_USD__c, Net_Proceeds_LC__c, Net_Proceeds_FC__c, Account__c From Sales_Forecasts__c];
                }
               
                else {
                    
                    Account a = new Account(Name='VisualForce2', End_Use__c='Nexar', Ship_Account_Region__c='JAPAN');
                    Insert a;
              
                }
                return sf;
              
    }
 
 
public PageReference page_search() {
       
         //get user selected record type
         sfRecordType = selectedRecordType;
        
         //get user selected accounting Month
         accountingMonth = selectedAccountingMonth;
        
         //getUserDefinedSF(accountingMonth, sfRecordType);
          getUserDefinedSF(sfRecordType);
        
         //getSF();
         sortField = 'Product2__c';
       
         doSort();
     
         return null;
     }
     
public void doSort(){
        String order = 'asc';
       
        //This checks to see if the same header was click two times in a row, if so it switches the order.
       
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
      
        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(sf,sortField,order);
    }
}


VP Page in next posting

DodiDodi

VF Page >>>>>


<apex:page controller="SalesAndForecastSort">
    <apex:form >
     
             <apex:pageblock title="Sales & Forecast Filter Criterea" id="theBlock">
            <apex:outputtext value="Which filter critera would you like to apply to the Sales and Forecasts record results?"/><BR></BR><BR><b>Type</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Accounting Month</b></BR><BR></BR>
                <apex:selectList value="{!selectedRecordType}" size="1" title="Type" >
                    <apex:selectOptions value="{!rtOptions}"/>
                </apex:selectList>&nbsp;
                                <apex:selectList value="{!selectedAccountingMonth}" size="1" title="Accounting Month" >
                    <apex:selectOptions value="{!accountingMonths}"/>
                </apex:selectList><p/>
                <apex:commandButton value="Search" action="{!page_search}" rerender="table"/>
         </apex:pageBlock>  
        
<apex:outputPanel id="table">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel >
                    <p>You have selected:</p>
                    <apex:dataList value="{!selectedRecordType}" var="c">{!c}</apex:dataList>
                    <apex:dataList value="{!selectedAccountingMonth}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
     
        <apex:pageBlock title="Results" >
            <apex:pageBlockTable value="{!sf}" var="o" id="table">
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Sales_Forecasts__c.Fields.Product2__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Product2__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Product2__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Sales_Forecasts__c.Fields.Net_Proceeds_USD__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Net_Proceeds_USD__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Net_Proceeds_USD__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Sales_Forecasts__c.Fields.Net_Proceeds_LC__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Net_Proceeds_LC__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Net_Proceeds_LC__c}"/>
                </apex:column>
               <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Sales_Forecasts__c.Fields.Net_Proceeds_FC__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Net_Proceeds_FC__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Net_Proceeds_FC__c}"/>
                </apex:column>
              </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>