+ Start a Discussion
William KeamWilliam Keam 

Testing CreatedDate that is in the past

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);
   }
  }
}






 
EnreecoEnreeco
I believe this is not doable. I suggest a workaround: split the query using dynamic SOQL and using the Test.isRunningTest() shape the query so that when executing in test context it returns results.

Hope this helps

--
May the Force.com be with you!