function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JamalRidaJamalRida 

How to deactivate users after inactive periode

Hello, i would like to know is there a way to deactivate users after an inactive period, as example if a user doesn't connect to salesforce within 2 weeks he become Inactive User, any help please !!!

best regards,

Best Answer chosen by Admin (Salesforce Developers) 
Vinita_SFDCVinita_SFDC

You can check for User login history in a query like: SELECT Status FROM LoginHistory, if it is "User is Inactive" then you can change the field isActive in users table from false to true, that will activate that user.

 

But while doing this you will have do a math on available licenses.

All Answers

Vinita_SFDCVinita_SFDC

Hello,

 

Query for last login time, SELECT LastLoginDate FROM User. Store this value in a variable, compare if it is more than two weeks old. If yes, then male isActive = false for that user in User table.

JamalRidaJamalRida

Hello thanks for the Answer, this is what i did too and it seems to be working

magicforce9magicforce9

Hi,

 

If its a one time job then Just copy paste this script in Execute Anonymous and it should do it for you(this isn't an efficient way thou).

List<User> users = [Select LastLoginDate , IsActive, Id From User where LastLoginDate < :Date.today().addDays(-14)];
for(User u : users)
{
	u.IsActive = False;
}
update users;

 

If you want to have a recorrung job that checks and deactivates users who didn't login from past 2 weeks then you need to put this in a scheduled batch job.

 

PS: Also note that if any of the deactivated users are approvers in any approval process, you'll need to change the approver to other user. You can't deactivate if the user is of default lead/case owner...etc.. For more details see: http://www.youtube.com/watch?v=UjauUkbjN3E

JamalRidaJamalRida

I have another question, lets suppose that we are able to deactivate these users, i would like to know if is there any way to reactivate the user onece he tries to login to salesforce , thanks for your help

best regards,

magicforce9magicforce9

I don't think we can automate activating user when he tries to login(Ideally a trigger on LoginHistory object could possibly do but it came'up with Save error: SObject type does not allow triggers: LoginHistory). Hence you need to manually look for LoginHistory record to see if the user ever tired to login. Another possibility is to do a daily/hourly batch job that can do the looking for you and send you an email with the list of users that tried to login in the last hour or so(but there will be delay in gettting user activated, its not going to be instant and the user will have to come back after next scheduled batch job had ran)

JamalRidaJamalRida

Hello again, 

i've got another idea and tell me if this is going to work : 

 

i'll create a permission set then i will check the Singe Sign On check box in System Permission, and i will assign all users to this permission set, now the question is if i have an inactive user is he going to be able to connecte to salesforce since he has this permission of Single sign on ???

Vinita_SFDCVinita_SFDC

Hello,

 

None of these ideas gonna work, once the user is inActive he/she can not login. He has to approach System administrator if he becomes inactive.

 

The reason why we have this functionality like this is suppose you have 10 licenses and with 10 active & 10 inactive users, then where will you accommodate the inactive users when they try to login.

 

 

JamalRidaJamalRida

Hello Thanks for the Answer,

 

so what you say means that there is no way to write a script or something to change the user from inactive to active onece he tries to login whether from Salesforce Login or external system Login ? 

Thanks for the Answer,

best regards

Vinita_SFDCVinita_SFDC

You can check for User login history in a query like: SELECT Status FROM LoginHistory, if it is "User is Inactive" then you can change the field isActive in users table from false to true, that will activate that user.

 

But while doing this you will have do a math on available licenses.

This was selected as the best answer
JamalRidaJamalRida

Okey Thank You :)