2

I am trying to check if a user is frozen but i can't figure out the relationship query. It seems like one doesn't exist even though there is a lookup. This older post hints at it but I can't figure out how to form my query. SOQL error with relationship

Tried all of these:

List<User> Users = new list<User>([
            SELECT Id,FirstName,LastName, (Select isFrozen FROM UserLogin)
            FROM User
            WHere LastLoginDate >= :SixtyDaysAgo
        ]);

      List<UserLogin> uLogins = new list<UserLogin>([
            Select Id, UserId, UserId.FirstName, UserId.LastName
            FROM UserLogin
            WHERE IsFrozen = FALSE
            AND LastLoginDate >= :SixtyDaysAgo
        ])

        List<UserLogin> uLogins = new list<UserLogin>([
            Select Id, UserId, (Select FirstName, LastName FROM User WHERE LastLoginDate >= :SixtyDaysAgo)
            FROM UserLogin
            WHERE IsFrozen = FALSE
        ])

1 Answer 1

5

Try a Left Inner Join:

SELECT FirstName, LastName FROM User
WHERE Id IN (SELECT UserId FROM UserLogin WHERE IsFrozen = true)

Also note that the query syntax returns a List<User>, and you are being excessively verbose. No need to do new List<User>([/*query*/]) where [/*query*/] will suffice.

4
  • 1
    Thanks for the syntax tip. Didn't realize that you could leave that off.
    – Matt M
    Sep 19, 2016 at 20:21
  • Do you think this code will work correctly? i am not sure how to test it.
    – Matt M
    Sep 19, 2016 at 20:48
  • //Get 60 Days Ago Date SixtyDaysAgo = date.today().addDays(-60); //Find users who have been inactive for sixty days so we can freeze their account. List<UserLogin> uLogins = [ SELECT ID FROM UserLogin WHERE UserId IN (Select Id,FirstName,LastName FROM User WHERE LastLoginDate <= :sixtyDaysAgo ) AND isFrozen = FALSE ];
    – Matt M
    Sep 19, 2016 at 20:48
  • 1
    @Matt No, you need a NOT IN (...) clause, but you should ask a separate question for that! :)
    – Adrian Larson
    Sep 19, 2016 at 20:51

You must log in to answer this question.

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