1

I wrote a small script in anonymous window in dev console to reset password for all users in the org apart from two. But it keeps throwing a error like -

System.InvalidParameterValueException: INSUFFICIENT_ACCESS: insufficient access rights on cross-reference id

 List<User> userList = new List<User>();
 userList = [SELECT Id from User WHERE IsActive = true and ID not in ('00561000000cdef','00561000001abcd')];
 for(User u : userList)
 {
    System.setPassword(u.Id, 'hello2017');
 }
0
0

This is because there are some users (e.g. Automate Process, System, Guest Users), which are default created by salesforce and you can't reset there password.

You need to exclude these users from your list, below is the example how you do it.

I have included only the default users created by salesforce, you need to check guest users also.

List<User> userList = new List<User>();
 userList = [SELECT Id from User WHERE IsActive = true and ID not in ('00561000000cdef','00561000001abcd') AND Name not in ('Automated Process','System')];
 for(User u : userList)
 {
    System.setPassword(u.Id, 'hello2017');
 }
0

It is better to use:

System.resetPassword( u.Id, true );

Check out here in the docs - much better this way and the user has to change the password upon the change. If you're running a class in dev console ( better than just execute anon code ) - Make sure the class is "without sharing" so your profile is not getting in the way for this - i still recommend using reset password instead of setting them all to a single password using setPassword

Not the answer you're looking for? Browse other questions tagged or ask your own question.