Tuesday, October 23, 2012

Can we set Salesforce user password?

As Salesforce administrator, we often get question from our user, "can you check what is my password? " hmmmm, some users often think like some legacy applications, where system admin able to see or modify Salesforce password.

In Salesforce is different, it is so secure, system admin and everyone else not able to see or modify any user password. But, as the best practice, we offer to reset their password, and Salesforce will send email to the user with instruction to set new password by  their own, and it should works fine.

But again, some users (read: senior management) not really want to bother by checking email and so on, even the request is come from his/her secretary. So how? No problem, we can change user email to her secretary email, secretary confirm email change in her email (although Salesforce also will email to the original email say that the email has been changed to new one), and reset password. It works, yay!!! 

But, how to change back the user email to the original one? The senior management need to click a link again in her email (she may have hundreds or thousands email never read). Problem.

So, I just start think if there is an easy way to set user password without have to go through such complicated process? It is worst when the user is going to present something in 5 minutes and we do not have much time to do above process. So, can we set user password?

YES, it is possible, this function is not exist in Salesforce setup menu, but Salesforce is kind enough to provide system administrator with Developer Console. Yes, Developer Console is for developer as it named, but admin also can use it for other purpose, like set user password, yay!!!

How? It is easy, just few clicks, copy and paste a short code below will do. Navigate to your name and click Developer Console menu.  It will open new window, Debug | Open Execute Anonymous Window, copy and paste code below to the Enter Apex Code windows and click Execute button. System.setPassword('00550000000rlrX', 'hello123');

In this sample: 00550000000rlrX is the User Id, if you are Salesforce admin, you should now this Id well, and hello123 is the new password to set.

But, if you still too lazy to check the User Id manually, and you have the username, you can just copy and paste statement below into Apex code execute box:
System.setPassword([SELECT Id FROM User WHERE username=''].Id, 'hello123');

Below sample code to set password of all active user in one go:

List<User> userList = new List<User>();
userList = [SELECT Id from User WHERE IsActive = true];
for (User u : userList)
    System.setPassword(u.Id, 'hello123');
    System.debug('DONE: ' + u.Id);

You can modify the SOQL above as needed, example: only for profile = "Sales Rep".

All statements above is apex code, but we can utilize it for other productive work as needed.