You need to sign in to do that
Don't have an account?
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>
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
Hi souvik9086, 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: