Randomly generated password for new accounts/ password reset script

ccepts
Mega Expert
Hello everyone,
Please I need your help to see what I may be missing
 
Here is my goal:
 
When a new user is created, I want to generate a random password so I can send this password to the user. The user will use the random password to reset the password and log in to the system.
 
Using the script below, I see a random password is created when I check the log. I have a custom field Pword, just for comparison. The generated temporary password shows on the log, but when I attempt to log in with the password, it doesn't work. This mean my password doesn't seem to be working Please help me evaluate.
 
Thank you
 
(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';

    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);

        randomstring += chars.substring(rnum, rnum + 1);

    }

    var password = current.user_password.setDisplayValue(randomstring);

        randomstring = current.user_password;

    current.password_needs_reset = true;
    current.update();
    var Pword = current.user_password;
    

    gs.log("password..   " + Pword);
    gs.log("randomstring..   " + randomstring);

})(current, previous);
 
 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ccepts 

you need to set the password as below; updated code below

Since this is before insert BR there is no need of current.update()

(function executeRule(current, previous /*null when async*/ ) {

// Add your code here

    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';

    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);

        randomstring += chars.substring(rnum, rnum + 1);

    }

  current.user_password.setDisplayValue(randomstring);
    current.password_needs_reset = true;

})(current, previous);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@ccepts 

you need to set the password as below; updated code below

Since this is before insert BR there is no need of current.update()

(function executeRule(current, previous /*null when async*/ ) {

// Add your code here

    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';

    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);

        randomstring += chars.substring(rnum, rnum + 1);

    }

  current.user_password.setDisplayValue(randomstring);
    current.password_needs_reset = true;

})(current, previous);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

I've tried using this solution but it's not working..
I've tried login using randomly generated password but it's giving an below error.

mayekarsalo_0-1755509483277.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

@ccepts 

Let me know if that answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader