function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wilbur07wilbur07 

Anonymous Block of Code

I keep getting compile errors on this piece of code which calculates time spent on support contracts:
 
Case[] allcases = [select Id, CreatedDate from Case];
Integer i=0;
Integer x=0;
Integer weekstime = 7*24*60*60*1000;
Integer daystime = 24*60*60*1000;
Integer hourstime = 60*60*1000;
Integer minutestime = 60000;
List<Contact> tbu = new Contact[ 0];
for (Case a: allcases) {
 Integer calctime=0;
 Integer numweeks=0;
 Integer remainder=0;
 x = System.now() - a.CreatedDate;
 numweeks = Math.floor(x/weekstime);
 calctime += 40*hourstime*numweeks;
 remainder = Math.mod(x,weekstime);
 Datetime finishweek = a.CreatedDate.addSeconds(remainder/1000);
 Datetime temptime = a.CreatedDate;
 while(temptime<finishweek) {
  Integer temptimehour = temptime.hour;
  if(temptime.format('dddd')=='Friday' && temptimehour>=17) temptime = temptime.addDays(2);
  else if(temptime.format('dddd')=='Saturday' && temptimehour<9) temptime = temptime.addDays(2);
  else if(temptime.format('dddd')=='Sunday' && temptimehour<9) temptime = temptime.addDays(1);
  else if(temptime.format('dddd')=='Saturday' && temptimehour>=9) temptime = temptime.addDays(1);
  else if(temptime.format('dddd')=='Sunday' && temptimehour>=9) temptime = temptime.addHours(8);
  else if(temptimehour< 8) temptime = temptime.addHours(1);
  else if(temptimehour>=17) temptime = temptime.addHours(8);
  else if(temptimehour>=8 && temptimehour<17) {
    if(temptimehour==8) temptime = temptime.addMinutes(1);
    else {
     if(temptimehour<17) {
      calctime += hourstime;
      temptime = temptime.addHours(1);
     }
     else {
      calctime += minutestime;
      temptime = temptime.addMinutes(1);
     }
    }
   }
 }
 a.SLA_Elapsed_Time__c = calctime;
 tbu.add(a);
}
update tbu;
 
I can't get past a couple of errors.  On the lines:
Datetime temptime = a.CreatedDate;
Integer temptimehour = temptime.hour;
 
I get an error that says "Initial term of field expression must be a concrete SObject: Datetime"
 
I thought I just declared the object to be Datetime???????????????
 
The second error I get is on the line:
 
x = System.Now() - a.CreatedDate;
 
It says "Date/time arithmetic expressions must use Integer and Double arguments"
 
I thought each datetime represented the amount of milliseconds since January 1 1970 or something like that, and were double types of variables themselves?????????
 
Can someone help me with these errors?
 
James
 
Ron HessRon Hess
Datetime temptime = a.CreatedDate;
Integer temptimehour = temptime.hour;

try

temptime.hour();



x = System.Now() - a.CreatedDate;
 
Please lookup the datetime methods in the developers guide, look for the method getTime()
it will give you  the number of milliseconds since January 1,
1970, 00:00:00 GMT represented by this DateTime
object


then you will be able to do math with that value

something like this
x = System.Now().getTime() - a.CreatedDate.getTime();