I need to get the user data who did not logged to system for last 30 days from a specific Date. Do you know how we can create a query for that? If you have any suggestions pls let me know.


  • 1
    Does this answer your question? SOQL date literals
    – McCubo
    Jan 30, 2023 at 8:56
  • Thank you @McCubo ,Let me check.
    – Basu
    Jan 30, 2023 at 9:02
  • Hi @McCubo, in this reffernce link gives the lastlogindate > SpecificDate,
    – Basu
    Jan 30, 2023 at 9:11

2 Answers 2


Not sure about literals ( can't see anything in the docs ).

However you can achieve this using the next code :

Date endInterval = Date.newInstance(2023, 1, 10);
Date startInterval = endInterval.addDays(-30);

List<Contact> conList = [Select Id,LastModifiedDate 
                            From Contact  
                            Where LastModifiedDate >: startInterval 
                            and LastModifiedDate <: endInterval ] ;

Depends on your needs you can include/exclude the first and the last day in the interval!

  • Thank you @Vitalii Dehnerys. Seems like it works
    – Basu
    Jan 30, 2023 at 9:50

Here's a SOQL query that will return Users who have not logged in the last 30 days:

SELECT Id, Name, LastLoginDate
FROM   User 
WHERE  LastLoginDate < LAST_N_DAYS:30

The more literal way to describe the above query is to say that it will only return Users whose last login date occurred more than 30 days ago.

You can also use the following SOQL query to return any Users who have not logged in between two specified dates:

SELECT Id, Name, LastLoginDate
FROM   User
                  FROM   LoginHistory
                  WHERE  LoginTime > 2023-01-15T00:00:00.000+0000
                  AND    LoginTime < 2023-01-31T23:59:59.000+0000 

Not the answer you're looking for? Browse other questions tagged .