3

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@email.com';
            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.

  • The problem here is first time you set password all will work fine but next time i guess since the password is resetted and you are attempting to set the same password system is not able to recognize.Try setting different one 2nd time and login.Let us know how it goes – Mohith Shrivastava Jun 23 '14 at 2:31
  • I have tried using different ones. I have also changed the password policy to not remember previous passwords so that it will allow me to do the some one over and over again. – Chris Jun 23 '14 at 3:18
  • Any update on this, were you able to resolve this ? – addy Aug 3 '16 at 16:36
  • They said that I cannot make these type of calls in a future context and updated the documentation to reflect this. I moved my process to a splash page that first resets the password and activates them and then continually tries to log them in using Javascript and a timer(the user stuff takes longer to run). Be warned though- This brought up issues with the login license and what exactly counts against a "login" for login based licenses. – Chris Aug 4 '16 at 19:53
0

I had this same issue. What I did is add one more code to render the user id again.

User u = new User();
u.lastname= 'blabla';
u.ContactId = currentContact.Id;
u.email='blabla@blabla.com';
...
...
...
system.setPassword(u.Id, 'password123');

User uu =[select id from user where email='blabla@blabla.com'];

return uu.id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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