+ Start a Discussion
LinThawLinThaw 

mass reset password

hi there,

Is there anyway to reset password for multiple users from apex?

the following can reset one user per transcation.
String newPassword = System.resetPassword(usrId ,false).getPassword();

Thanks in advance
LinThaw.
Arvind KumarArvind Kumar
Hi Lin,

You can use below code for reset password of multiple users.
public class ChangePassword 
{
    public changepassword()
    {
        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 have to execute code it through developer console.
It is easy, just a few clicks, copy and paste a shortcode below will do. Navigate to your name and click Developer Console menu.  It will open a new window, Debug | Open Execute Anonymous Window, copy and paste the code below to the Enter Apex Code windows and click Execute button.
 
changepassword ch= new changepassword();

You have to execute above (changepassword ch= new changepassword();)  in the Open Execute Anonymous Window. With this help, All multiple users password has been changed.

Please implement it. 

Thanks,

Arvind Kumar

LinThawLinThaw
Thanks Arvind Kumar,

It can be get Governor error if there are above 150 active users.
How do you think?

Regards,
LinThaw
Arvind KumarArvind Kumar
Hi Lin,

It will work for all 150 active users. You will not get any error because we are using one soql query in this code.

So try it.

Thanks,
Arvind Kumar
 
LinThawLinThaw
Thanks Arvind Kumar,
Yes, It will work for all 150 active users.

If so there is no way for more than 150 users?

System.LimitException: Too many DML statements: 151

Regards,
LinThaw​
Arvind KumarArvind Kumar
Hi Lin,

Don't take a tension.

It will work for more than 150 active users. 
surasura
no it won't work for more than 150 users as each call to set or reset password is  considered a one dml statement. ithe only way to archive this for more than 150 users via apex is to use batch apex. 

In your excute9) mehod call the method to reset password  and use a batch size less than 150 when you all the batch,
LinThawLinThaw
If it is possible to implement,
please let me share more info.
Thanks.
Regards,
LinThaw.
Sahoo RajeshSahoo Rajesh

System.InvalidParameterValueException: UNKNOWN_EXCEPTION: invalid repeated password