I am experiencing an issue creating users and setting their password. I have a need to setup a great deal of users but only activate them as they attempt to login. The creation process and activation process is working fine but I am getting a strange error when attempting to login. I initially create a list of users, insert them, then set their password, then deactivate them. For now I am just attempting to get this to set to password123 until I get this to work.
I can run a script in the Developer Console that will allow me to set the password and only once I run that code in the dev console am I able to login. I tried creating a button to set the password and also an update based on a checkbox that sets the user to the same password. I cannot login to the user until I run the setPassword command inside of the developer console.
Here is the code that does this and all is done in a future method by itself
for(Contact currentContact : contacts){
User u = new User();
u.isActive = true;
u.ContactId = currentContact.Id;
u.LastName = currentContact.LastName;
u.Alias = currentContact.LastName.substring(0,7);
u.Email = '[email protected]';
u.EmailEncodingKey='UTF-8';
u.LanguageLocaleKey='en_US';
u.LocaleSidKey='en_US';
u.TimeZoneSidKey='America/New_York';
u.ProfileId = '0000000000asj1';
u.UserName = currentContact.Attribute__c + '@email.com';
u.isActive = true;
userInsertList.add(u);
}
if(!Test.isRunningTest()){
insert userInsertList;
}
for(User currentUser : userInsertList){
if(currentUser.Id != null){
System.setPassword(currentUser.Id,'password123');
}
}
for(User currentUser: userInsertList){
currentUser.IsActive = false;
}
if(!Test.isRunningTest()){
update userInsertList;
}
Here is the code that activates and resets the user's password
if(user.IsActive == false){
system.debug('Activating user');
user.IsActive = true;
update user;
try{
system.setPassword(user.Id, 'password123');
}
catch(InvalidParameterValueException e){
system.debug(e.getMessage());
}
}
Custom Link code on user object:
webservice static String doPasswordReset(String userId){
System.setPassword(userId,'Password123');
return '';
}
Reading this similar stack exchange post I noticed that he is experiencing the same issue but I don't think that the answers there really answer the question. IF this was something I would have to wait on why does the Developer Console allow me to succesfully run the code and the other ways of running the code does not work. I can see in the Audit Trail that the password is being reset each time I try to run it using all the various methods.
Any help would be greatly appreciated
Edit* Another thing to note is that when I did change the password policy to remember passwords it would prevent me from making it "password123" - throwing an InvalidParameterException something along the lines of previous passwords cannot be used. So it would appear that the password is being changed correctly and the audit trail does show that my calls to set password are the last call that is made.
UPDATE: I have support looking into my issue they are also experiencing the same issue with the setPassword method not being able to be called from the Future context. I have currently implemented a Schedulable/Batchable process to resolve the issue.