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
NEW.ax1607NEW.ax1607 

List index out of bounds: 0

Hello,

 

when I try open my chart in a tab I get the following error because my gerId =null;

List index out of bounds: 0

 

I don't know how to pass my account Id to Controller/ visualforce page, someone can help me please? thank you

 

My code:


public with sharing class Gauge2ChartController {
public List<Account> a;// = new Account(Name='');
public boolean render {get; set;}
public Integer currentYear = date.Today().year();
public Date last12Months = date.Today().addMonths(-12);
public Integer totalRevenue {get; set;}
public ID gerId {get; set;}

//Essa Classe é usada para mostrar o Gráfico nas accounts
//Grabbing the Accountid from the current record//
//Quando usamos ApexPages.StandardController controller no construtor quer dizer qu vamos chamar a classe como um extensions
//na classe visualforce page
public Gauge2ChartController(ApexPages.StandardController controller) {

render=true;

try{
        a = [SELECT Id, Name, Site, owner.name, phone,billingstreet,billingstate,billingcountry,billingcity FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
                   system.debug('testeprimeiro' + a);
}
catch(Exception ex1){
System.Debug('We did not find the Account' + ex1);
}
               
                  
        
}

public List<gaugeData> getData() {
     Integer TotalOpptys = 0;
     Integer TotalAmount = 0;
          Integer thisMonth = date.Today().month();
           
            system.debug('gerID ' + gerId);
          a = [SELECT Id, Name, Site, owner.name, phone,billingstreet,billingstate,billingcountry,billingcity FROM Account
                  WHERE Id = :gerId];
                   
                   system.debug('testeprimeiro' + a);
                  
           if(a.isEmpty()){
               ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: List is empty.');
           }
           

          AggregateResult ClosedWonOpptys = [select SUM(Amount) totalRevenue, CALENDAR_YEAR(CloseDate) theYear, COUNT(Name) numOpps
                                   from Opportunity
                                  where Accountid = :a.get(0).Id and StageName = 'Closed Won'
                                   and CALENDAR_YEAR(CloseDate) = :currentYear
                                   GROUP BY CALENDAR_YEAR(CloseDate) LIMIT 1];
            system.debug('testeterceiro' + ClosedWonOpptys);
            
          List<gaugeData> data = new List<gaugeData>();
          data.add(new gaugeData(Integer.valueOf(ClosedWonOpptys.get('numOpps')) + ' Opps', Integer.valueOf(ClosedWonOpptys.get('totalRevenue'))));
          system.debug('testeterceiro' + data);
           
          return data;
           
           
     }

    public class gaugeData {
        public String name { get; set; }
        public Integer size { get; set; }

        public gaugeData(String name, Integer data) {
            this.name = name;
            this.size = data;
        }
    }
    
}

 

Visaulaforce page:

 

<apex:page StandardController="Account" extensions="Gauge2ChartController" tabStyle="Chart__tab" >

    <apex:chart name="MyChart" height="250" width="400" animate="true" data="{!data}">
        <apex:axis type="Gauge" position="gauge" title="Closed Won Opportunities"  minimum="0" maximum="50000" steps="10"/>
        <apex:gaugeSeries dataField="size" donut="50" colorSet="#78c953,#ddd"/>
    </apex:chart>

     <script>
        MyChart.on('beforeconfig', function(config) {
            config.axes[0].margin=-10;
        });
       
    </script>
    
 </apex:page>

 

souvik9086souvik9086

Declare the list as new. Modified code is

 

public with sharing class Gauge2ChartController {
public List<Account> a = new List<Account>();
public boolean render {get; set;}
public Integer currentYear = date.Today().year();
public Date last12Months = date.Today().addMonths(-12);
public Integer
totalRevenue {get; set;}
public ID gerId {get; set;}

//Essa Classe é usada para mostrar o Gráfico nas accounts
//Grabbing the Accountid from the current record//
//Quando usamos ApexPages.StandardController controller no construtor quer dizer qu vamos chamar a classe como um extensions
//na classe visualforce page
public Gauge2ChartController(ApexPages.StandardController controller) {

render=true;

try{
        a = [SELECT Id, Name, Site, owner.name, phone,billingstreet,billingstate,billingcountry,billingcity FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
                   system.debug('testeprimeiro' + a);
}
catch(Exception ex1){
System.Debug('We did not find the Account' + ex1);
}
               
                  
        
}

public List<gaugeData> getData() {
     Integer TotalOpptys = 0;
     Integer TotalAmount = 0;
          Integer thisMonth = date.Today().month();
           
            system.debug('gerID ' + gerId);
          a = [SELECT Id, Name, Site, owner.name, phone,billingstreet,billingstate,billingcountry,billingcity FROM Account
                  WHERE Id = :gerId];
                   
                   system.debug('testeprimeiro' + a);
                  
           if(a.isEmpty()){
               ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: List is empty.');
           }
           

          AggregateResult ClosedWonOpptys = [select SUM(Amount) totalRevenue, CALENDAR_YEAR(CloseDate) theYear, COUNT(Name) numOpps
                                   from Opportunity
                                  where Accountid = :a.get(0).Id and StageName = 'Closed Won'
                                   and CALENDAR_YEAR(CloseDate) = :currentYear
                                   GROUP BY CALENDAR_YEAR(CloseDate) LIMIT 1];
            system.debug('testeterceiro' + ClosedWonOpptys);
            
          List<gaugeData> data = new List<gaugeData>();
          data.add(new gaugeData(Integer.valueOf(ClosedWonOpptys.get('numOpps')) + ' Opps', Integer.valueOf(ClosedWonOpptys.get('totalRevenue'))));
          system.debug('testeterceiro' + data);
           
          return data;
           
           
     }

    public class gaugeData {
        public String name { get; set; }
        public Integer size { get; set; }

        public gaugeData(String name, Integer data) {
            this.name = name;
            this.size = data;
        }
    }
    

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

NEW.ax1607NEW.ax1607

Hi , thnk you for your reply.
Now i don't have error message but I still don't have the account ID.

 

When a make debug log I've this result:

 

SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|select Id, Name, Site, owner.name, phone, billingstreet, billingstate, billingcountry, billingcity from Account where Id = :tmpVar1
10:56:13.040 (40211000)|SOQL_EXECUTE_END|[19]|Rows:0

10:56:13.040 (40371000)|USER_DEBUG|[21]|DEBUG|testeprimeiro()
10:56:13.040 (40378000)|SYSTEM_METHOD_EXIT|[21]|System.debug(ANY)
10:56:13.040 (40386000)|SYSTEM_MODE_EXIT|false