manually setup a users password in Salesforce

Manually Setup a Users Password in Salesforce

Manually Setup a User’s Password in Salesforce using Developer Console or the Workbench.

There are times when an Administrator needs to manually setup a users password in Salesforce. Salesforce Administrator’s can reset a User’s password from the User record detail page but Salesforce user interface doesn’t allow an Administrator to setup a specific password. Here is an Awesome Admin Hack to achieved this using the Developer Console or the Workbench.

Let’s discuss each method step by step.

Manually Setup a User’s Password in Salesforce Using the Developer Console.

Step 1. Get the User’s Id from the URL bar on the User’s record detail page.

get Salesforce User ID
get Salesforce User ID

Step 2. Open Developer Console

Step 3. Open the Execute Anonymous Window from Debug >> Open Execute Anonymous Window OR using the shortcut Ctrl + E

Step 4. Enter the following Apex code. Replace the UserId and Password with the actual values we need.

System.SetPassword('UserId','Password'); //Replace the UserId and Password

Step 5. Click Execute

We successfully manually setup the User’s password using the developer console and an email with the new security token will is emailed to the User.

 

Manually Setup a User’s Password in Salesforce Using Workbench.

  1. Visit Workbench (https://workbench.developerforce.com/login.php) and Login using your Salesforce credentials.
  2. Navigate to Utilities >> Password Management
  3. Enter the User’s Id and the New Password
  4. Click on Change Password

We successfully manually setup the User’s password using Workbench and an email with the new security token will is emailed to the User.

 

Pro Tip: Creating a User Interface to manually setup a User’s password in Salesforce.

Step 1. Create a New Text Field on the User Object and Name it Password__c

Step 2. Create the following trigger on the User object

trigger setPassword on User (before update) {
    for (User u:Trigger.New) {
        User oldUser = Trigger.OldMap.get(u.id);
        If ((u.Password__c != null) && (oldUser.Password__c != u.Password__c)) {
            System.SetPassword(u.id,u.password__c);
            //Uncomment below line to clear password field
            //u.Password__c = null;
        }    
    }
}

Note: Use validation rules on Password__c to only accept passwords that align with your organization’s password policies.

Here’s how the trigger looks in the developer console (image)

manually setup a users password in Salesforce
Manually setup a users password in Salesforce

Step 3. Now visit the User’s record detail page and enter the new password in the Password field that we created and click Save.

The User’s password will be changed and an email with the new security token will be emailed to the User.

 

Update: To send the new password to the user by email, the trigger will be:

trigger CPTsetPassword on User (before update) {
    List <String> toAddresses = New List <String>();
    for (User u:Trigger.New) {
        User oldUser = Trigger.OldMap.get(u.id);
        If ((u.Password__c != null) && (oldUser.Password__c != u.Password__c)) {
            System.SetPassword(u.id,u.password__c);
            toAddresses.add(u.Email);
            //Uncomment below line to clear password field after reset
            //u.Password__c = null;   
            
            //Sending the users new password by email
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
            mail.setSubject('Salesforce Password Reset');
            mail.setPlaintextBody('Your password was reset by the Administrator and the new Password is ' + u.Password__c);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});         
        }    
    } 
}

 

Comments

2 responses to “Manually Setup a Users Password in Salesforce”

  1. himanshu Avatar
    himanshu

    Hi,
    Thank you for posting this hacks for Password. This is going to solve a big problem for us.

    I have created Trigger based on your code. Do you have a Test class for this?

    Thanks,
    Himanshu

    1. Taha Syed Avatar

      Something like this:
      @isTest
      public class setPasswordTest {
      static testMethod void setThePassword() {
      User U = [Select id from user limit 1];
      U.Password__c = ‘Test124’;
      Update U;
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.