4

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 Apr 26 '16 at 13:19
  • Yes @Pavantej I couldn't find in Time base workflow this field. – user24737 Apr 26 '16 at 15:25
7
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;
        usersToUpdate.add(u);
}

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. – Vigneshwaran G Apr 26 '16 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 Apr 26 '16 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 Apr 26 '16 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 Apr 26 '16 at 15:47
  • Hi @karti I tried to run the query which you mentioned in the code. But "unknown error passing query". – user24737 Apr 26 '16 at 17:59
1

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.

http://www.jitendrazaa.com/blog/salesforce/how-to-use-login-flow-in-salesforce/

I thinks it resolves your problem @chris

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy