+ Start a Discussion
Jon-Michael MurpheyJon-Michael Murphey 

Unexpected Token on setting Datetime Variable

I have a schedule apex that deactivates users after 90 days since last login, the apex failed on run because of an Unexpected token error on the following: First error: unexpected token: 'dt'

Please review the snippit below:

        dateTime dt = date.today()-90;
       public String query = 'SELECT Name, LastLoginDate, Id From User WHERE IsActive = true AND LastLoginDate < +dt  and profile<>"System Adminstrator" ';
@anilbathula@@anilbathula@
Hi Jon-Michael,

Try this code:-

date dt=System.today().addDays(-90);  

String query = 'SELECT Name, LastLoginDate, Id From User WHERE IsActive = true AND LastLoginDate<dt  and profile<>"System Adminstrator" ';

Thanks
Anil.B
Narender Singh(Nads)Narender Singh(Nads)

Hi Jon,
Try this:
date dt=System.today().addDays(-90);  
String query = 'SELECT Name, LastLoginDate, Id From User WHERE IsActive = true AND LastLoginDate<:dt  and profile<>"System Adminstrator" ';

Or you can also write your query as
date dt=System.today().addDays(-90);  
String query = 'SELECT Name, LastLoginDate, Id From User WHERE IsActive = true AND LastLoginDate<'+dt+'  and profile<>"System Adminstrator" ';

Which ever suits your requirement better.

Let me know if it helps.
Thanks.

Jon-Michael MurpheyJon-Michael Murphey
@anilbathula@
That didnt work it threw a new error: First error: expecting a colon, found 'dt'