Set User Password Using Apex in Salesforce
Sample Code:
//Get User User u = [SELECT Id From User Where Username = 'test@testuser.com']; //Set Password system.setPassword(u.Id, 'Pass@1234');
Sample Code:
//Get User User u = [SELECT Id From User Where Username = 'test@testuser.com']; //Set Password system.setPassword(u.Id, 'Pass@1234');
In Salesforce there are two methods to set password and to reset password for a user.
resetPassword(Id userId, Boolean sendUserEmail)
: This method resets the password for the specified user. When the user logs in with the new password, they are prompted to enter a new password, and to select a security question and answer if they haven’t already. If you specify true for sendUserEmail
, the user is sent an email notifying them that their password was reset. A link to sign onto Salesforce using the new password is included in the email.
setPassword(Id userId, String password)
: Sets the password for the specified user. When the user logs in with this password, they are not prompted to create a new password.
Sample Code:
List<User> users = new List<User>(); String username = 'test@test.com'; users = Database.Query('SELECT Id, Name FROM User WHERE UserName =: username'); for(User u : users){ //For reset User password System.resetPassword(u.Id, true); //For set User password //System.setPassword(u.Id, 'Test@1234'); }