A Salesforce Developer team was developing a Salesforce application that required the ability to reset and change user passwords programmatically.
To do this, I created an Apex class. Here, I will explain how to reset and change user password using Salesforce Apex.
Reset Current User Password Using Salesforce Apex
In the following Apex program, I explain how we can reset the currently logged-in user’s password using Salesforce Apex.
Here, I created an Apex class using the resetCurrentUserPass() method. In that method, we retrieve the current user ID using the UserInfo class and the GetUserId() method.
- SOQL: To get the current user’s details, we query for the user and pass the fetched ID in the query.
- Try block: In the try block, I execute the logic; here, we want to reset the user’s password. To reset the password, we have the system.resetPassword(UserID, SendEmailToUser) method, and in that method, we need to pass the user ID and a boolean value. If the boolean value is set to
true, Salesforce will send an email to the user with the password reset link. - Catch block: The catch block will execute if there is an issue resetting the user’s password, displaying the message we entered.
Then, execute this apex code in the anonymous window.
public class RstCurrentUserPass {
public void resetCurrentUserPass(){
Id userID = UserInfo.GetuserId();
User currentUser = [Select Id, Name From User WHERE Id =: userID];
try{
system.resetPassword(currentUser.id,true);
system.debug('The password for ' +currentUser.Name + ' has been successfully
reset');
}
catch(Exception e){
e.setmessage('There is a problem to change the user password');
}
}
}
After the code is executed, you will see the message that we entered in the try block. It will display only when the password is successfully reset.
Then, you will get an email from Salesforce support with the reset link. Clicking the link will give you a reset button, allowing you to change your password.

In this way, you can reset the user’s password for the currently logged-in user using Salesforce Apex.
Reset and Change User Password Using Salesforce Apex
You can also change the user password when you reset it. When you change the password immediately after resetting the program, you do not need to reset it manually.
In the following program, I will explain how to change a currently logged-in user’s password using Apex in Salesforce.
To change the user password, you also need to obtain the user ID. Then, using a SOQL query, get the user details and execute logic in the try block. Here, we are changing the user password for the currently logged-in user.
For that, we have the system.setPassword(User id, ‘String password’) method in the Salesforce Apex. In that method, we need to pass the User ID and the new password that you want to set for the particular user.
Then, in the catch block, enter an error message so that when the try block fails, we will get a catch block message. After that, execute the code in an anonymous window.
try{
system.setPassword(currentUser.id, 'NewPassword@123');
system.debug('The password for ' +currentUser.Name + ' has been successfully
changed');
}
catch(Exception e){
e.setmessage('There is a problem to change the user password');
}
finally{} In the following image, you can see the reset password and change password method I added in the single apex program, so that when we reset the password, the new password gets set for that particular user.

As we execute the code, you can see that the message has been displayed successfully, which means our try block has been executed successfully.

Then, you will get an error when you try to log in to your Salesforce org with your old password. Here, you need to enter the password that you passed to the system.setPassword() method.

In this way, we can reset and change the currently logged-in user’s password using Apex in Salesforce.
Reset and Change User Password by User ID in Salesforce Apex
Now, I will explain how to reset and change the user password for a specific user. To do this, we need the User ID of that particular user. We can obtain a User ID from SOQL or use the CASESAFEID function.
In the above apex program, we have seen the reset and change of the currently logged-in user’s password. In this Apex program, I explained how to reset another user’s password using a User ID.
Here, I created a class and method, then a variable UserID with ID datatype, and stored the user ID for which I want to reset and set a new password.
Next, I used the system.resetPassword() method to reset the password, and then proceeded to the next system.setPassword() method, I set a different password for that particular user.
Then, save and execute the program.
public class ResetPassByUserID {
public void ResetUserPassByID(){
ID UserID = '005J3000000h8DlIAI';
User userdetails = [Select Id, Name From User WHERE Id =: UserID];
try{
system.resetPassword( UserID, False );
system.debug( 'The password for ' + userdetails.Name + ' has been successfully reset' );
}
catch(Exception e){
e.setmessage( 'There is a problem to reset the user password' );
}
Finally{}
try{
system.setPassword( UserID, 'NewPassword@1234' );
system.debug( 'The password for ' + userdetails.Name + ' has been successfully reset' );
}
catch( Exception e ){
e.setmessage( 'There is a problem to Change the user password' );
}
Finally{}
}
}
When another user, for whom we have changed the password, logs in with a new password, they will receive a verification code via email. They must enter this code to verify their identity.

After verification, they will get logged in to the Salesforce org.

In this way, we can change another user’s password by using the user ID in Salesforce Apex.
Conclusion
I hope you have an idea about creating Salesforce users from Apex. I explained how to reset and change user passwords from Salesforce Apex for currently logged-in users using the UserInfo class and getUserId() method, and for other users using the UserID for that particular user.
You may like to read:
- Get Current User Info Using Apex in Salesforce
- Variables and Data Types in Salesforce Apex
- Get Current Datetime in Apex Salesforce
- Select How to Connect VS Code to Salesforce Org
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.