5

I have to deactivate users whose last login is greater than 30 days. I am using workflow but how it will meet Evaluation criteria like 1.Created, 2.created, and every time it's edited.

Is it possible through Time-Dependent Workflow Actions ? Will it run automatically on time ?

2 Answers 2

7

You can't use a workflow rule for this, because logging in doesn't trigger workflow rules. Instead, you'll want to schedule a regular check to deactivate users:

public class DeactivateInactiveUsers implements Schedulable {
    public void execute(SchedulableContext context) {
        User[] selectedUsers = [SELECT Id FROM User WHERE IsActive = TRUE AND Id NOT IN (SELECT UserId FROM LoginHistory WHERE LoginTime = LAST_N_DAYS:30)];
        for(User record: selectedUsers) {
            record.IsActive = false;
        }
        Database.update(selectedUsers, false);
    }
}

Simply put this into your org, and set a daily schedule, and you're set.

3
  • Is there chances of 50001 exception if LoginHistory has more rows? Dec 16, 2016 at 14:03
  • 3
    @PranayJaiswal Not unless you've got more than 50,000 users you need to deactivate in one go. The sub-query filter doesn't count towards the 50,000 limit. If you were overly concerned about that, you could segue from scheduled apex to batch apex, but that's probably overkill. Of course, if you need to deactivate more than 10,000 users, you'll want to go with batchable or queueable apex anyways, since that's the DML limit.
    – sfdcfox
    Dec 16, 2016 at 14:08
  • My Bad. I kinda missed that out. :) Dec 16, 2016 at 14:32
1

Can this not be done using Login Flows rather than workflows?

https://developer.salesforce.com/page/Login-Flows

You must log in to answer this question.

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