I want an automated process that needs to inactivate users which are not logged in last 120 days. I tried with Time Based Workflow but i couldn't find Last login field. Is there any way that we can achieve.

  • Have you tried to look at this field. LastLoginDate in user object @Eswaraaro
    – Pavan tej
    Commented Apr 26, 2016 at 13:19
  • Yes @Pavantej I couldn't find in Time base workflow this field.
    – user24737
    Commented Apr 26, 2016 at 15:25

2 Answers 2

List<User> usersToUpdate = new List<User>();
for(User u : [Select id, IsActive from User where  isActive=true and lastLoginDate <= :DATE.TODAY()-120]){
        u.IsActive = false;

if (usersToUpdate.size()>0){
    update usersToUpdate;

There are some considerations to do this:

-- one cannot deactivate self user record

-- Cannot deactivate a user record which is referenced in another user record in a hierarchy custom field

-- Make a Schedule Class apex and invoke via schedule UI in Salesforce. Every night for example.

  • You can make the soql more selective by including another where condition filtering based on lastlogindate like Select id, IsActive from User where isActive=true and lastlogindate <= 120daysbefore where 120daysbefore is Date.today()-120. Commented Apr 26, 2016 at 14:54
  • Hi @Karti, Thanks for your help. I think it will not inactivate users automatically. If yes where do i invoke this code?. Actually I need to send an email before inactivate and then inactivate. This should done automatically.
    – user24737
    Commented Apr 26, 2016 at 15:05
  • @chris - Invoke this code in developer console.. To send Email update, use Messaging.sigleemailmessage method to send email from developer console by saving the list of email addresses in a string.
    – kso
    Commented Apr 26, 2016 at 15:35
  • 2
    @Chris - you cannot use Workflow/ Process builder to do this... If u want to run this at regular intervals, Create a scheduled apex class and use the above code to achieve the result.... And if the users list is very large (i.e., more than 10000) use a batch class and schedule the batch class..
    – kso
    Commented Apr 26, 2016 at 15:47
  • Hi @karti I tried to run the query which you mentioned in the code. But "unknown error passing query".
    – user24737
    Commented Apr 26, 2016 at 17:59

No, unfortunately that is not possible.

But it can possible in two ways.

1.you can develop apex code with soql.We can easily get the last login date.

2.Even if you want to do with configuration.We can achieve this with Flows.

I'm sharing a link please look on to these.


I thinks it resolves your problem @chris

You must log in to answer this question.