• William Keam
  • NEWBIE
  • 30 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 9
    Replies
Hello everyone,

I have hit a wall and need help on how to test @future methods which are called from a trigger.

I think it has something do with the fact that @future methods are no necessarily processed immediately and as such my assertions are not passing, any help on how to get my code covered would be greatly appreciated,

What the trigger does is reassigns open and waiting cases to a queue when a user is disabled.

The trigger:
trigger user_deactivated_reassign_case on User (before update) {
    
    for(User u : trigger.new){
        User oldUsr = Trigger.oldMap.get(u.Id);
        if(oldUsr.IsActive==TRUE && u.IsActive==FALSE){
            User newUsr = Trigger.newMap.get(u.Id);
            string recordIds = u.Id;
            user_deactivation_reassign_case_class.reassign_case(recordIds);
        }
    }
}

The @future Apex Class:
public class user_deactivation_reassign_case_class {
    @future
    public static void reassign_case(string recordIds){
        List<Case> cas = [SELECT Id, Type FROM Case WHERE OwnerId=:recordIds];
        List<Case> casesTobeUpdated = new List<Case>();
               
        for(Case currentCase : cas){
            if(currentCase.Status=='Open'||currentCase.Status=='Waiting Response'||currentCase.Status=='On Hold'){
                if(currentCase.Type=='AU.COM'||currentCase.Type=='Customer Service'){
                    currentCase.OwnerId='00G90000001mq6F'; //Premium Support L2
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Management'){
                    currentCase.OwnerId='00G90000001mq5m'; //Management
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Administration'||currentCase.Type=='Domain Dispute'){
                    currentCase.OwnerId='00G90000001mq65'; //Premium Admin
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='EDU.AU'||currentCase.Type=='GOV.AU'||currentCase.Type=='Corporate'){
                    currentCase.OwnerId='00G90000001mzrl'; //Corporate
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='VPS Unmanaged'||currentCase.Type=='VPS Managed'){
                    currentCase.OwnerId='00G90000001nJfA'; //VPS Unmanaged
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Sales'||currentCase.Type=='Social Media Sales'||currentCase.Type=='Online Marketing Sales'){
                    currentCase.OwnerId='00G90000001mzrH'; //Retail Sales
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Online Marketing Support'){
                    currentCase.OwnerId='00G90000001mzrR'; //Online Marketing Support
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Web Design'){
                    currentCase.OwnerId='00G90000001mzT4'; //Web Design
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Website Security'){
                    currentCase.OwnerId='00G90000001nAl0'; //Website Security
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Credit Control'){
                    currentCase.OwnerId='00G90000001nAlK'; //Credit Control
                    casesTobeUpdated.add(currentCase);
                }
                else{
                    currentCase.OwnerId='00G90000001mq6F'; //Premium Support L2
                    casesTobeUpdated.add(currentCase);
                }
            }
        }
    update casesTobeUpdated;
    }
}

And the test class which I can't figure out:
@isTest
public class user_deactivation_reassign_case_test {
    public static testmethod void test_user_termination(){
        User thisUser = [SELECT Id FROM USER WHERE Name='System Automation'];
        
        system.runAs(thisUser){
        User u = new User(FirstName='Test',
                          LastName='Test',
                          Username='apextest@netregistry.com', 
                          Email='apextest@netregistry.com', 
                          Alias='test', 
                          CommunityNickname='test', 
                          TimeZoneSidKey='Australia/Sydney', 
                          LocaleSidKey='en_US', 
                          EmailEncodingKey='UTF-8', 
                          ProfileId='00e90000001NAum', 
                          LanguageLocaleKey='en_US');
        
        insert u;
        Account acc = new Account(Account_Ref__c='1',Name='Test', Reseller_Id__c=1, Virtualisation_Id__c=1);
        insert acc;
        Case cas = new Case (AccountId=acc.Id,OwnerId=u.Id,Type='AU.COM',Status='Open',Brand__c='Netregistry');
        insert cas;
        update u;
        string recordIds = u.Id;

        system.assertEquals(cas.OwnerId, '00G90000001mq6F');
        }
    }
}


I have read other posts saying I need to use Test.startTest(); and Test.stopTest(); but I can't figure out where this fits into a @future class being called via a trigger
Hi guys,

I have been unable to figure out how to successfully test an apex class which uses CreatedDate = LAST_MONTH

I guess what I need to be shown is how to insert a record with a created date in the past, I read about using test.loaddata but was unable to do anything besides countng how many records were loaded while the test ran, I also tried to figure out json deserializing but I wasnt able to figure it out

I searched all over google but was unable to find a simple example of how to do it and so I come to you for help

Any and all help is greatly appreciated

Here is my class:
public class NPS_ZIP_Last_Month{

    public double all{public get; public set;}
    public double notlikely{public get; public set;}
    public double neutral{public get; public set;}
    public double extremelylikely{public get; public set;}
    public double promoters {public get; public set;}
    public double detractors {public get; public set;}
    public double NPS {public get; public set;}
    public NPS_ZIP_Last_Month(ApexPages.StandardController controller) {
    
    List <GetFeedback__c> allnotlikely=[Select CreatedDate, NPS__c from GetFeedback__c where Brand__c = 'Ziphosting' and CreatedDate = LAST_MONTH and
    (NPS__c=0 or 
    NPS__c=1 or 
    NPS__c=2 or 
    NPS__c=3 or 
    NPS__c=4 or 
    NPS__c=5 or 
    NPS__c=6) LIMIT 10000];

    double d=0.0;
    integer notlikely=0;
    if(allnotlikely.size()>0){
    for(GetFeedback__c c: allnotlikely)
    {
     d+=c.NPS__c;
     notlikely++;
    }
    }

    List <GetFeedback__c> allneutral=[Select CreatedDate, NPS__c from GetFeedback__c where Brand__c = 'Ziphosting' and CreatedDate = LAST_MONTH and
    (NPS__c=7 or 
    NPS__c=8) LIMIT 10000];

    double e=0.0;
    integer neutral=0;
    if(allneutral.size()>0){
    for(GetFeedback__c c: allneutral)
    {
     e+=c.NPS__c;
     neutral++;
    }
    }
  
    List <GetFeedback__c> allextremelylikely=[Select CreatedDate, NPS__c from GetFeedback__c where Brand__c = 'Ziphosting' and CreatedDate = LAST_MONTH and
    (NPS__c=9 or 
    NPS__c=10) LIMIT 10000];

    double f=0.0;
    integer extremelylikely=0;
    if(allextremelylikely.size()>0){
    for(GetFeedback__c c: allextremelylikely)
    {
     f+=c.NPS__c;
     extremelylikely++;
    }
    }

   //Calculate NPS making sure not to divide by zero
   if(extremelylikely==0&&notlikely==0){
   promoters=0;
   detractors=0;
   all=0;
   NPS=0;
   }
   else if(extremelylikely==0&&notlikely>0){
   extremelylikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=0;
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely==0){
   notlikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=0;
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely>0){
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
  }
}






 
Hello everyone,

I have an apex class that is used to calculate the Net Promoter Score based on surveys returned back into a custom object in salesforce, the code works and calculates the correct values but I am unsure on how to get started writing test code for the class.  The code is as follows:

public class NPScalc{
    public NPScalc(ChartController controller) {
    }
    public double all{public get; public set;}
    public double notlikely{public get; public set;}
    public double neutral{public get; public set;}
    public double extremelylikely{public get; public set;}
    public double promoters {public get; public set;}
    public double detractors {public get; public set;}
    public double NPS {public get; public set;}
    public NPScalc(ApexPages.StandardController controller) {
    
    List <GetFeedback__c> allnotlikely=[Select CreatedDate, NPS__c from GetFeedback__c where
    (NPS__c=0 or 
    NPS__c=1 or 
    NPS__c=2 or 
    NPS__c=3 or 
    NPS__c=4 or 
    NPS__c=6 or 
    NPS__c=6) LIMIT 10000];

    double d=0.0;
    integer notlikely=0;
    if(allnotlikely.size()>0){
    for(GetFeedback__c c: allnotlikely)
    {
     d+=c.NPS__c;
     notlikely++;
    }
    }

    List <GetFeedback__c> allneutral=[Select CreatedDate, NPS__c from GetFeedback__c where
    (NPS__c=7 or 
    NPS__c=8) LIMIT 10000];

    double e=0.0;
    integer neutral=0;
    if(allneutral.size()>0){
    for(GetFeedback__c c: allneutral)
    {
     e+=c.NPS__c;
     neutral++;
    }
    }
  
    List <GetFeedback__c> allextremelylikely=[Select CreatedDate, NPS__c from GetFeedback__c where
    (NPS__c=9 or 
    NPS__c=10) LIMIT 10000];

    double f=0.0;
    integer extremelylikely=0;
    if(allextremelylikely.size()>0){
    for(GetFeedback__c c: allextremelylikely)
    {
     f+=c.NPS__c;
     extremelylikely++;
    }
    }

   //Calculate NPS making sure not to divide by zero
   if(extremelylikely==0&&notlikely==0){
   promoters=0;
   detractors=0;
   all=0;
   NPS=0;
   }
   else if(extremelylikely==0&&notlikely>0){
   extremelylikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=0;
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely==0){
   notlikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=0;
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely>0){
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
  }
}
Any pointers, advice or anything would be greatly appreciated!
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;

        }
    }
}

Hello,

I have an apex class which I am using to calculate the average age time taken to resolve cases.  I have written a test class for the code but I am stuck at 93% coverage, any help would be greatly appreciated!

Heres my apex class:
public class avgCase{

    private final Case cas;

    public double avrgTime{public get; public set;}
    public double count{public get; public set;}
    public double d{public get; public set;}
    
    public avgCase(ApexPages.StandardController controller) {

    this.cas = (Case)Controller.getRecord();
    List <case> allCase=[Select Case_Duration__c from case where accountid=:cas.accountid and status='Resolved'];

    double d=0.0;
    integer count=0;
    if(allCase.size()>0){
    for(case c: allCase)
    {
     d+=c.Case_Duration__c;
     count++;
    }
    }
    if(count>0){
        avrgTime=d/count;
    }
    else
    {
        avrgTime=0;  //This is the line which is not getting code coverage
    }
    }
}

Here is my test class:
@isTest
public class avgCase_test{
    static testMethod void testAvgCase() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=15243, Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',AccountID=a.id,status='Resolved');
        insert c;
        ApexPages.StandardController sc = new ApexPages.standardController(c);
        avgCase testMe = new avgCase(sc);
        testMe.d=1;
        testMe.count=1;
        testMe.avrgTime=1;
        ApexPages.currentPage();
        System.AssertEquals(1, testme.count);
        System.AssertEquals(testme.avrgTime, 1);
	}   
}


Thanks in advance for all the help!!

Hello everyone,

I need a hand getting 100% code coverage for one of my Apex Classes

The Apex Class:
public class avgCES{

    private final Case cas;

    public double avrgCES{public get; public set;}
    public string CES_Colour{public get; public set;}
    public double count{public get; public set;}

    public avgCES(ApexPages.StandardController controller) {

    this.cas = (Case)Controller.getRecord();
    

if(cas.account.id!=NULL){
    
    List <case> allCase=[Select How_easy_was_it_to_get_the_help_you_need__c from case where accountid=:cas.account.id and How_easy_was_it_to_get_the_help_you_need__c!=NULL];

    double d=0.0;
    integer count=0;
    if(allCase.size()>0){
    for(case c: allCase)
    {
     d+=c.How_easy_was_it_to_get_the_help_you_need__c;
     count++;
    }
    }
    
    
    if(count>0)
    {
        avrgCES=d/count;
    }
    else
    {
        avrgCES=0;
        CES_Colour='Grey';
    }
    
    if(avrgCES<6)
    {
        CES_Colour='Red';
    }
    else if(avrgCES>=6 && avrgCES <=8 )
    {
        CES_Colour='Yellow';
    }
    else
    {
        CES_Colour='Green';
    }
}
}
}

The Test Class
@isTest
public class avgCES_test{
    public static testMethod void testAvgCES() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=6);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=5);
        insert c;
        insert c2;
        ApexPages.StandardController sc = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc);
        testme.avrgCES=5.5;
        testme.CES_Colour='Red';
        
        system.assertEquals(5.5, testme.avrgCES);
        system.assertEquals('Red', testme.CES_Colour);
  }
    
    public static testMethod void testAvgCES2() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=10);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8);
        insert c;
        insert c2;
        ApexPages.StandardController sc2 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc2);
        testme.avrgCES=9;
        
        system.assertEquals(9, testme.avrgCES);
        system.assertEquals('Green', testme.CES_Colour);
  }
    
  public static testMethod void testAvgCES3() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=7);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8);
        insert c;
        insert c2;
        ApexPages.StandardController sc3 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc3);
        testme.avrgCES=7.5;
    
        testme.CES_Colour='Yellow';
        system.assertEquals(7.5, testme.avrgCES);
        system.assertEquals('Yellow', testme.CES_Colour);
  }
    
  public static testMethod void testAvgCES4() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL);
        insert c;
        insert c2;
        ApexPages.StandardController sc4 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc4);
        testme.avrgCES=0;
        testme.CES_Colour='Grey';
        system.assertEquals(0, testme.avrgCES);
        system.assertEquals('Grey', testme.CES_Colour);
  }
    

}


Im still fairly new to Apex and would appreciate any and all help with this.

Thanks in advance!
Hi guys,

I have been unable to figure out how to successfully test an apex class which uses CreatedDate = LAST_MONTH

I guess what I need to be shown is how to insert a record with a created date in the past, I read about using test.loaddata but was unable to do anything besides countng how many records were loaded while the test ran, I also tried to figure out json deserializing but I wasnt able to figure it out

I searched all over google but was unable to find a simple example of how to do it and so I come to you for help

Any and all help is greatly appreciated

Here is my class:
public class NPS_ZIP_Last_Month{

    public double all{public get; public set;}
    public double notlikely{public get; public set;}
    public double neutral{public get; public set;}
    public double extremelylikely{public get; public set;}
    public double promoters {public get; public set;}
    public double detractors {public get; public set;}
    public double NPS {public get; public set;}
    public NPS_ZIP_Last_Month(ApexPages.StandardController controller) {
    
    List <GetFeedback__c> allnotlikely=[Select CreatedDate, NPS__c from GetFeedback__c where Brand__c = 'Ziphosting' and CreatedDate = LAST_MONTH and
    (NPS__c=0 or 
    NPS__c=1 or 
    NPS__c=2 or 
    NPS__c=3 or 
    NPS__c=4 or 
    NPS__c=5 or 
    NPS__c=6) LIMIT 10000];

    double d=0.0;
    integer notlikely=0;
    if(allnotlikely.size()>0){
    for(GetFeedback__c c: allnotlikely)
    {
     d+=c.NPS__c;
     notlikely++;
    }
    }

    List <GetFeedback__c> allneutral=[Select CreatedDate, NPS__c from GetFeedback__c where Brand__c = 'Ziphosting' and CreatedDate = LAST_MONTH and
    (NPS__c=7 or 
    NPS__c=8) LIMIT 10000];

    double e=0.0;
    integer neutral=0;
    if(allneutral.size()>0){
    for(GetFeedback__c c: allneutral)
    {
     e+=c.NPS__c;
     neutral++;
    }
    }
  
    List <GetFeedback__c> allextremelylikely=[Select CreatedDate, NPS__c from GetFeedback__c where Brand__c = 'Ziphosting' and CreatedDate = LAST_MONTH and
    (NPS__c=9 or 
    NPS__c=10) LIMIT 10000];

    double f=0.0;
    integer extremelylikely=0;
    if(allextremelylikely.size()>0){
    for(GetFeedback__c c: allextremelylikely)
    {
     f+=c.NPS__c;
     extremelylikely++;
    }
    }

   //Calculate NPS making sure not to divide by zero
   if(extremelylikely==0&&notlikely==0){
   promoters=0;
   detractors=0;
   all=0;
   NPS=0;
   }
   else if(extremelylikely==0&&notlikely>0){
   extremelylikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=0;
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely==0){
   notlikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=0;
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely>0){
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
  }
}






 
Hello everyone,

I have an apex class that is used to calculate the Net Promoter Score based on surveys returned back into a custom object in salesforce, the code works and calculates the correct values but I am unsure on how to get started writing test code for the class.  The code is as follows:

public class NPScalc{
    public NPScalc(ChartController controller) {
    }
    public double all{public get; public set;}
    public double notlikely{public get; public set;}
    public double neutral{public get; public set;}
    public double extremelylikely{public get; public set;}
    public double promoters {public get; public set;}
    public double detractors {public get; public set;}
    public double NPS {public get; public set;}
    public NPScalc(ApexPages.StandardController controller) {
    
    List <GetFeedback__c> allnotlikely=[Select CreatedDate, NPS__c from GetFeedback__c where
    (NPS__c=0 or 
    NPS__c=1 or 
    NPS__c=2 or 
    NPS__c=3 or 
    NPS__c=4 or 
    NPS__c=6 or 
    NPS__c=6) LIMIT 10000];

    double d=0.0;
    integer notlikely=0;
    if(allnotlikely.size()>0){
    for(GetFeedback__c c: allnotlikely)
    {
     d+=c.NPS__c;
     notlikely++;
    }
    }

    List <GetFeedback__c> allneutral=[Select CreatedDate, NPS__c from GetFeedback__c where
    (NPS__c=7 or 
    NPS__c=8) LIMIT 10000];

    double e=0.0;
    integer neutral=0;
    if(allneutral.size()>0){
    for(GetFeedback__c c: allneutral)
    {
     e+=c.NPS__c;
     neutral++;
    }
    }
  
    List <GetFeedback__c> allextremelylikely=[Select CreatedDate, NPS__c from GetFeedback__c where
    (NPS__c=9 or 
    NPS__c=10) LIMIT 10000];

    double f=0.0;
    integer extremelylikely=0;
    if(allextremelylikely.size()>0){
    for(GetFeedback__c c: allextremelylikely)
    {
     f+=c.NPS__c;
     extremelylikely++;
    }
    }

   //Calculate NPS making sure not to divide by zero
   if(extremelylikely==0&&notlikely==0){
   promoters=0;
   detractors=0;
   all=0;
   NPS=0;
   }
   else if(extremelylikely==0&&notlikely>0){
   extremelylikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=0;
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely==0){
   notlikely=0;
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=0;
   NPS=math.FLOOR(promoters-detractors);
   }
   else if(extremelylikely>0&&notlikely>0){
   all=extremelylikely+neutral+notlikely;
   promoters=math.FLOOR((extremelylikely/all)*100);
   detractors=math.FLOOR((notlikely/all)*100);
   NPS=math.FLOOR(promoters-detractors);
   }
  }
}
Any pointers, advice or anything would be greatly appreciated!
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;

        }
    }
}

Hello,

I have an apex class which I am using to calculate the average age time taken to resolve cases.  I have written a test class for the code but I am stuck at 93% coverage, any help would be greatly appreciated!

Heres my apex class:
public class avgCase{

    private final Case cas;

    public double avrgTime{public get; public set;}
    public double count{public get; public set;}
    public double d{public get; public set;}
    
    public avgCase(ApexPages.StandardController controller) {

    this.cas = (Case)Controller.getRecord();
    List <case> allCase=[Select Case_Duration__c from case where accountid=:cas.accountid and status='Resolved'];

    double d=0.0;
    integer count=0;
    if(allCase.size()>0){
    for(case c: allCase)
    {
     d+=c.Case_Duration__c;
     count++;
    }
    }
    if(count>0){
        avrgTime=d/count;
    }
    else
    {
        avrgTime=0;  //This is the line which is not getting code coverage
    }
    }
}

Here is my test class:
@isTest
public class avgCase_test{
    static testMethod void testAvgCase() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=15243, Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',AccountID=a.id,status='Resolved');
        insert c;
        ApexPages.StandardController sc = new ApexPages.standardController(c);
        avgCase testMe = new avgCase(sc);
        testMe.d=1;
        testMe.count=1;
        testMe.avrgTime=1;
        ApexPages.currentPage();
        System.AssertEquals(1, testme.count);
        System.AssertEquals(testme.avrgTime, 1);
	}   
}


Thanks in advance for all the help!!

Hello everyone,

I need a hand getting 100% code coverage for one of my Apex Classes

The Apex Class:
public class avgCES{

    private final Case cas;

    public double avrgCES{public get; public set;}
    public string CES_Colour{public get; public set;}
    public double count{public get; public set;}

    public avgCES(ApexPages.StandardController controller) {

    this.cas = (Case)Controller.getRecord();
    

if(cas.account.id!=NULL){
    
    List <case> allCase=[Select How_easy_was_it_to_get_the_help_you_need__c from case where accountid=:cas.account.id and How_easy_was_it_to_get_the_help_you_need__c!=NULL];

    double d=0.0;
    integer count=0;
    if(allCase.size()>0){
    for(case c: allCase)
    {
     d+=c.How_easy_was_it_to_get_the_help_you_need__c;
     count++;
    }
    }
    
    
    if(count>0)
    {
        avrgCES=d/count;
    }
    else
    {
        avrgCES=0;
        CES_Colour='Grey';
    }
    
    if(avrgCES<6)
    {
        CES_Colour='Red';
    }
    else if(avrgCES>=6 && avrgCES <=8 )
    {
        CES_Colour='Yellow';
    }
    else
    {
        CES_Colour='Green';
    }
}
}
}

The Test Class
@isTest
public class avgCES_test{
    public static testMethod void testAvgCES() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=6);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=5);
        insert c;
        insert c2;
        ApexPages.StandardController sc = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc);
        testme.avrgCES=5.5;
        testme.CES_Colour='Red';
        
        system.assertEquals(5.5, testme.avrgCES);
        system.assertEquals('Red', testme.CES_Colour);
  }
    
    public static testMethod void testAvgCES2() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=10);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8);
        insert c;
        insert c2;
        ApexPages.StandardController sc2 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc2);
        testme.avrgCES=9;
        
        system.assertEquals(9, testme.avrgCES);
        system.assertEquals('Green', testme.CES_Colour);
  }
    
  public static testMethod void testAvgCES3() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=7);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8);
        insert c;
        insert c2;
        ApexPages.StandardController sc3 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc3);
        testme.avrgCES=7.5;
    
        testme.CES_Colour='Yellow';
        system.assertEquals(7.5, testme.avrgCES);
        system.assertEquals('Yellow', testme.CES_Colour);
  }
    
  public static testMethod void testAvgCES4() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL);
        insert c;
        insert c2;
        ApexPages.StandardController sc4 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc4);
        testme.avrgCES=0;
        testme.CES_Colour='Grey';
        system.assertEquals(0, testme.avrgCES);
        system.assertEquals('Grey', testme.CES_Colour);
  }
    

}


Im still fairly new to Apex and would appreciate any and all help with this.

Thanks in advance!