+ Start a Discussion
William KeamWilliam Keam 

Help with APEX class

Hello,

I am in the process implementing my first APEX chart into my org and have run into a wall that I can't seem to get through.

I am receiving the following error when attempting to save the code:

Error: Compile Error: Variable does not exist: cas.Account.Id at line 18 column 73

Any help at all would be greatly appreciated.

public class ChartController {
    public Case cas { get; set; }
    // Return a list of data points for a chart
    public List<Data> getData() {
        return ChartController.getChartData();
    }
    public ChartController(ApexPages.StandardController sc) {
        this.cas = (Case)sc.getRecord();
    }
    
    public static List<Data> getRemoteData() {
        return ChartController.getChartData();
    }

    public static List<Data> getChartData() {
        List<Data> data = new List<Data>();

        Integer jan =[SELECT count() FROM Case WHERE Case.Account.Id = :cas.Account.Id AND status='Resolved' AND CALENDAR_MONTH(ClosedDate)=1];
        data.add(new Data('Jan', jan));
        Integer feb =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=2];
        data.add(new Data('Feb', feb));
        Integer mar =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=3];
        data.add(new Data('Mar', mar));
        Integer apr =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=4];
        data.add(new Data('Apr', apr));
        Integer may =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=5];
        data.add(new Data('May', may));
        Integer jun =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=6];
        data.add(new Data('Jun', jun));
        Integer jul =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=7];
        data.add(new Data('Jul', jul));
        Integer aug =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=8];
        data.add(new Data('Aug', aug));
        Integer sep =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=9];
        data.add(new Data('Sep', sep));
        Integer oct =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=10];
        data.add(new Data('Oct', oct));
        Integer nov =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=11];
        data.add(new Data('Nov', nov));
        Integer dec =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=12];
        data.add(new Data('Dec', dec));
        return data;
    }
    
    // Wrapper class
    public class Data {
        public String name { get; set; }
        public Integer data1 { get; set; }

        public Data(String name, Integer data1) {
            this.name = name;
            this.data1 = data1;

        }
    }
}

logontokartiklogontokartik
Integer jan =[SELECT count() FROM Case WHERE AccountId = :cas.AccountId AND status='Resolved' AND CALENDAR_MONTH(ClosedDate)=1];
 
 Hi  I am not sure if it's the right field, it's accountID, can you try using cas.accountID , your where clause on line 18 should be 
William KeamWilliam Keam
Hi,

Thanks for the suggestion unfortunately I received a similar error

Error: Compile Error: Variable does not exist: cas.AccountId at line 18 column 73

logontokartiklogontokartik
Ok can you confirm if you have access to Account field on Case? I think there is no reason otherwise for the query to fail.
William KeamWilliam Keam
Hi,

I have another APEX class that uses the same ":cas.Account.Id" and it works without a problem.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
public class ChartController {
public Case cas { get; set; }
// Return a list of data points for a chart
public chartcontroller()
{
cas=new case();
}
public List<Data> getData() {
return ChartController.getChartData();
}
public ChartController(ApexPages.StandardController sc) {
this.cas = (Case)sc.getRecord();
}
public static List<Data> getRemoteData() {
return ChartController.getChartData();
}
public static List<Data> getChartData() {
List<Data> data = new List<Data>();
Integer jan =[SELECT count() FROM Case WHERE Case.Account.Id = :cas.Account.Id AND status='Resolved' AND CALENDAR_MONTH(ClosedDate)=1];
data.add(new Data('Jan', jan));
Integer feb =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=2];
data.add(new Data('Feb', feb));
Integer mar =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=3];
data.add(new Data('Mar', mar));
Integer apr =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=4];
data.add(new Data('Apr', apr));
Integer may =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=5];
data.add(new Data('May', may));
Integer jun =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=6];
data.add(new Data('Jun', jun));
Integer jul =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=7];
data.add(new Data('Jul', jul));
Integer aug =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=8];
data.add(new Data('Aug', aug));
Integer sep =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=9];
data.add(new Data('Sep', sep));
Integer oct =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=10];
data.add(new Data('Oct', oct));
Integer nov =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=11];
data.add(new Data('Nov', nov));
Integer dec =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=12];
data.add(new Data('Dec', dec));
return data;
}
// Wrapper class
public class Data {
public String name { get; set; }
public Integer data1 { get; set; }
public Data(String name, Integer data1) {
this.name = name;
this.data1 = data1;
}
}
}
Try this code
William KeamWilliam Keam
Hi Vidhyasagaran,

I tried your code but it is still throwing the same error.

Error: Compile Error: Variable does not exist: cas.Account.Id at line 19 column 65

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
public class ChartController {
public Case cas { get; set; }
// Return a list of data points for a chart
public List<Data> getData() {
return ChartController.getChartData();
}
public ChartController(ApexPages.StandardController sc) {
this.cas = (Case)sc.getRecord();//if this code doesnot work check this line is correct or not
cas=new case();
}
public static List<Data> getRemoteData() {
return ChartController.getChartData();
}
public static List<Data> getChartData() {
List<Data> data = new List<Data>();
Integer jan =[SELECT count() FROM Case WHERE Account.Id = :cas.Account.Id AND status='Resolved' AND CALENDAR_MONTH(ClosedDate)=1];
data.add(new Data('Jan', jan));
Integer feb =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=2];
data.add(new Data('Feb', feb));
Integer mar =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=3];
data.add(new Data('Mar', mar));
Integer apr =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=4];
data.add(new Data('Apr', apr));
Integer may =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=5];
data.add(new Data('May', may));
Integer jun =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=6];
data.add(new Data('Jun', jun));
Integer jul =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=7];
data.add(new Data('Jul', jul));
Integer aug =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=8];
data.add(new Data('Aug', aug));
Integer sep =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=9];
data.add(new Data('Sep', sep));
Integer oct =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=10];
data.add(new Data('Oct', oct));
Integer nov =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=11];
data.add(new Data('Nov', nov));
Integer dec =[SELECT count() FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=12];
data.add(new Data('Dec', dec));
return data;
}
// Wrapper class
public class Data {
public String name { get; set; }
public Integer data1 { get; set; }
public Data(String name, Integer data1) {
this.name = name;
this.data1 = data1;
}
}
}
try this code if it doesnot work then check this line-this.cas = (Case)sc.getRecord();
sunny522sunny522
Hi Kean,

Try the below modified code

public class ChartController{
    public Case cas { get; set; }
    // Return a list of data points for a chart
 
    public ChartController(ApexPages.StandardController sc) {
        this.cas = (Case)sc.getRecord();
    }
 

    public void getChartData() {
      
         List<Data> data = new List<Data>();
        list<case> jan =[SELECT id FROM Case WHERE Account.Id = :cas.AccountId AND status='Resolved' AND CALENDAR_MONTH(ClosedDate)=1];
        data.add(new Data('Feb', jan.size()));
        list<case> feb =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=2];
        data.add(new Data('Feb', feb.size()));
        list<case> mar =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=3];
        data.add(new Data('Mar', mar.size()));
        list<case> apr =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=4];
        data.add(new Data('Apr', apr.size()));
        list<case> may =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=5];
        data.add(new Data('May', may.size()));
        list<case> jun =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=6];
        data.add(new Data('Jun', jun.size()));
        list<case> jul =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=7];
        data.add(new Data('Jul', jul.size()));
        list<case> aug =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=8];
        data.add(new Data('Aug', aug.size()));
        list<case> sep =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=9];
        data.add(new Data('Sep', sep.size()));
        list<case> oct =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=10];
        data.add(new Data('Oct', oct.size()));
        list<case> nov =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=11];
        data.add(new Data('Nov', nov.size()));
        list<case> dec =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=12];
        data.add(new Data('Dec', dec.size()));
       
        }
       
     public class Data {
        public String name { get; set; }
        public Integer data1 { get; set; }

        public Data(String name, Integer data1) {
            this.name = name;
            this.data1 = data1;

        }
    }   
}

we need to store query result in that particular object and then we need to get size of that list by using size().


let me know if u need further help..


if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
William KeamWilliam Keam
Hi Sunny,

Thanks for the updated code, it is now able to compile!

However now I am having problems displaying the data on a chart, can you take a look at my visualforce code and let me know where the problem lies?

<apex:page standardController="Case" extensions="ChartController">
    <apex:chart height="170" width="200" data="{!case}">
          <apex:axis type="Numeric" position="left" fields="data1" title="Cases Resolved" grid="true"/>
          <apex:axis type="Category" position="bottom" fields="name" title="Month"/>
        <apex:lineSeries axis="left" fill="true" xField="name" yField="data1" markerType="cross" markerSize="4" markerFill="#FF0000"/>
   </apex:chart>
</apex:page>

When I load the visualforce page (which is on a case) it does not display a chart and does not show any error messages.
sunny522sunny522
Hi Kean,
    go through the modified apex code and visualforce page code.


apex class:

public class ChartController{
    public Case cas { get; set; }
    // Return a list of data points for a chart

    public ChartController(ApexPages.StandardController sc) {
        this.cas = (Case)sc.getRecord();
    }


    public static List<Data> getChartData() {
     
         List<Data> data = new List<Data>();
        list<case> jan =[SELECT id FROM Case WHERE Account.Id = :cas.AccountId AND status='Resolved' AND CALENDAR_MONTH(ClosedDate)=1];
        data.add(new Data('Feb', jan.size()));
        list<case> feb =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=2];
        data.add(new Data('Feb', feb.size()));
        list<case> mar =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=3];
        data.add(new Data('Mar', mar.size()));
        list<case> apr =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=4];
        data.add(new Data('Apr', apr.size()));
        list<case> may =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=5];
        data.add(new Data('May', may.size()));
        list<case> jun =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=6];
        data.add(new Data('Jun', jun.size()));
        list<case> jul =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=7];
        data.add(new Data('Jul', jul.size()));
        list<case> aug =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=8];
        data.add(new Data('Aug', aug.size()));
        list<case> sep =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=9];
        data.add(new Data('Sep', sep.size()));
        list<case> oct =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=10];
        data.add(new Data('Oct', oct.size()));
        list<case> nov =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=11];
        data.add(new Data('Nov', nov.size()));
        list<case> dec =[SELECT id,casenumber FROM Case WHERE status='Resolved' AND CALENDAR_MONTH(ClosedDate)=12];
        data.add(new Data('Dec', dec.size()));

        return data;
      
        }
      
     public class Data {
        public String name { get; set; }
        public Integer data1 { get; set; }

        public Data(String name, Integer data1) {
            this.name = name;
            this.data1 = data1;

        }
    }  
}


visualforce page code:

<apex:page standardController="Case" extensions="ChartController">
    <apex:chart height="170" width="200" data="{!ChartData}">
          <apex:axis type="Numeric" position="left" fields="data1" title="Cases Resolved" grid="true"/>
          <apex:axis type="Category" position="bottom" fields="name" title="Month"/>
        <apex:lineSeries axis="left" fill="true" xField="name" yField="data1" markerType="cross" markerSize="4" markerFill="#FF0000"/>
   </apex:chart>
</apex:page>


let me know if you need further help...

if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.