This is untested so you may need to have play, however this should return a List of the RevenueWrapper classes which contains the 4 columns you are looking to use in your VF page:
public class RevenueController{
Map<String, RevenueWrapper> ResultList = new Map<String, RevenueWrapper>();
public RevenueController(){
List<AggregateResult> result = [
SELECT FISCAL_YEAR(Date__c) Date, Type__c, sum(Amount__c) Amount
FROM Revenue__c
GROUP BY FISCAL_YEAR(Date__c), Type__c];
for(AggregateResult ar : result){
RevenueWrapper RW = new RevenueWrapper();
//If the RevenueWrapper already exists grab it from the Map
if(ResultList.containsKey(String.valueOf(ar.get('Date')))){
RW = ResultList.get(String.valueOf(ar.get('Date')));
}
//Set the amount
RW.setAmount(ar);
//Save back to the Map
ResultList.put(String.valueOf(ar.get('Date')), RW);
}
}
public List<RevenueWrapper> getResults() {
return ResultList.values();
}
public class RevenueWrapper{
public String revenueDate { get; set; }
public Integer budgetAmount { get; set; }
public Integer actualAmount { get; set; }
public Integer varianceFYPercent { set; }
public Integer varianceFYAmount { set; }
public RevenueWrapper(){
}
public void setAmount(AggregateResult ar){
revenueDate = String.valueOf(ar.get('Date'));
//Set the amount value based on type
if(ar.get('Type__c') == 'Budget'){
budgetAmount = Integer.valueOf(ar.get('Amount'));
}else if(ar.get('Type__c') == 'Actual'){
actualAmount = Integer.valueOf(ar.get('Amount'));
}else{ //Catch all for a type that isn't recognised
return;
}
}
public Integer getVarianceFYAmount(){
return actualAmount - budgetAmount;
}
public Decimal getVarianceFYPercent(){
if(budgetAmount != 0){
return Decimal.valueOf((Double.valueOf(actualAmount) / Double.valueOf(budgetAmount)) * 100).setscale(1);
} else{
return 0;
}
}
}
}
You can't instantiate a List
like that using an integer. You don't need to explicitly specify how much items are going into the list when it is created. Instead, just remove the integer:
//Instantiate the list
String[] myArray = new List<String>();
You're logic in your loop seems to be incorrect as well. Specifically, myArray
's size will always be 0 when you instantiate it. Change your loop logic to:
for(Integer i=0;i<length;i++) {
// code
}
You want to generate a List of strings based on the i
variable. You will want to change your internal logic to:
//Populate the array
myArray.add('Test ' + i);
// Write value to the debug log
System.debug(myArray[i]);
Finally, you want to return that Array. You need to change your method so the return type is no longer void
by changing its signature:
public static String[] generateStringArray(Integer length)
then it is just a matter of return
ing that array at the end of your method:
return myArray;
This would bring it all together as:
public class StringArrayTest {
//Public Method
public static String[] generateStringArray(Integer length) {
//Instantiate the list
String[] myArray = new List<String>();
//Iterate throught the list
for(Integer i=0;i<length;i++) {
//Populate the array
myArray.add('Test ' + i);
// Write value to the debug log
System.debug(myArray[i]);
} //end loop
return myArray;
}//end method
}// end class
Some more info on Arrays and Lists, Loops, and Class methods for future reference.
Best Answer
Your comment states:
Your method:
}
should be (to return just one value) - assuming, as per your comment, that one record will always exist (otherwise, extend to include error handling)