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
Rebecca Hendricks 9Rebecca Hendricks 9 

Time Between Activities Trigger Has Errors - Unable to Save Code

Hello. I'm trying to create a report for my boss that shows the number of Activities that were created within 30 minutes of the previous activity for each of our reps.  Having done some research on this, I thought the best way to go about it would be to have a custom field (checkbox) that would indicate if the task was created within 30 minutes of the previous activity, and another custom field (checkbox) that would indicate if the task was the first activity of the day.  (If it's the first activity modification of the day, I don't want it to be counted as not being within 30 minutes of the last activity modification, as the Rep wasn't on duty to make any modifications.)

I was trying to create a trigger to be "ran" anytime an Activity was created/modified that would execute SOQl to get the most recently modified task, and then compare the information to set the two flags previously mentioned.  I looked at previous apex codes to get the syntax as I'm new to Apex.  The "copied" syntax compiled and saved in the previous versions, but my current code is giving me a lot of errors on the Problems tab of Developer console.

Here's my code.  (I know proper code shouldn't be heavily commented, but I did it to assist you to follow what I am trying to accomplish as you can't see my org structure, as well as help any future non-coding background employees know what was going on, which may be very possible with my employer).
trigger UpdateActivityFlags on Task (before insert, before update) {
    
    Boolean within30Mins = new Boolean();	//determine if this task was created before within 30 mins of last activity
    String userID = UserInfo.getUserId();	//Get User Id for task owner
    DateTime rightNow = system.now();	//get the date/time of when the task is being created
    Date rightNowDate = date.newInstance(rightNow.year(), rightNow.month(), rightNow.day())	//date part only of date/time task is being created
    
    //Need to get most recent activity information
    Task mostRecentTask = [SELECT * FROM Task Where OwnerId = userID AND LastModifiedTime__c < rightNow ORDER BY LastModifiedTime__c Desc Limit 1];
    
    Date mostRecentTaskDate = date.newInstance(mostRecentTask.LastModifiedTime__c.year(), mostRecentTask.LastModifiedTime__c.month()
                                               , mostRecentTask.LastModifiedTime__c.day());	//gets date portion of most recent activity modification
    
    if(((rightNow - mostRecentTask.LastModifiedTime)*24*60) <= 30){	//this task was created within 30 mins of last activity modification
        within30Mins = True;
    }
    
    //update flag information
    for (Task t : Trigger.New){
        t.CreationWithin30MinsOfLastTask = within30Mins;
        if((rightNowDate - mostRecentTaskDate) <> 0){	//task creating and last activity modification are not on the same day
            FirstActivityOfDay = True;
        }
    }
    
}

The errors I'm getting are:
  • Type cannot be constructed: Boolean on line 3
  • Unexpected token 'rightNowDate' on Line 6
  • Unexpted token '=' on line 6
  • Unexpected toke '(' on line 6
  • Unexpected token 'mostRecentTask' on line 9
  • Unexpected token '=' on line 9 (I actually have 2 separate errors on this one)
  • Unexpected token '[' on line 9
  • Unexpected token 'Where' on line 9
  • Unexpected token 'AND' on line 9
  • Unexpected token '<' on line 9
  • Unexpected token 'ORDER' on line 9
  • Unexpected token 'BY' on line 9
  • Unexpected token 'Desc' on line 9
  • Unexpected token '1' on line 9
  • Variable does not exist: mostRecentTask.LastModifiedTime__c on lines 11 & 12
  • Variable does not exist: mostRecentTask on line 14
  • Variable does not exist: CreationWithin30MinsOfLastTask on line 20
  • Variable does not exist: rightNowDate on line 21
  • Variable does not exist: FirstActivityOfDay on line 22
I understand what the errors are stating (it can't find the variable, it can't be created, etc.), but I don't understand why they are being thrown as the syntax is the same as other code that has passed just fine.  Help would be very much appreciated.
Alain CabonAlain Cabon
  • Missing semicolons at the end of the lines.
  • Use of formula expression in Apex (substraction of dates instead of daysBetween).
  • Missing colons into SOQL expressions for the binds (local code variable within a SOQL expression).
  • Use of a java expression (new Boolean()).
  • Missing "__c" for the custom fields.
trigger UpdateActivityFlags on Task (before insert, before update) {
    
    Boolean within30Mins = False;	//determine if this task was created before within 30 mins of last activity
    // within30Mins = Boolean.valueOf('false');
    String userID = UserInfo.getUserId();	//Get User Id for task owner
    DateTime rightNow = system.now();	//get the date/time of when the task is being created
    Date rightNowDate = date.newInstance(rightNow.year(), rightNow.month(), rightNow.day());	//date part only of date/time task is being created
    
    //Need to get most recent activity information
    Task mostRecentTask = [SELECT id FROM Task Where OwnerId = :userID AND LastModifiedDate < :rightNow ORDER BY LastModifiedDate Desc Limit 1];
    
    Date mostRecentTaskDate = date.newInstance(mostRecentTask.LastModifiedDate.year(), mostRecentTask.LastModifiedDate.month()
                                               , mostRecentTask.LastModifiedDate.day());	//gets date portion of most recent activity modification
    
    if(Date.valueOf(rightNow).daysBetween(Date.valueOf(mostRecentTask.LastModifiedDate)) <= 30) {	//this task was created within 30 mins of last activity modification
        within30Mins = True;
    }
    
    //update flag information
    for (Task t : Trigger.New){
        t.CreationWithin30MinsOfLastTask__c = within30Mins;
        if(Date.valueOf(rightNow).daysBetween(mostRecentTaskDate)) != 0){	//task creating and last activity modification are not on the same day
            FirstActivityOfDay__c = True;
        }
    }   
}

LastModifiedDate should be replaced with  LastModifiedTime__c  yet again.