Sailesh4
Giga Expert

Hi All,

Recently, I have created a functionality where you can generate automated passwords and send notifications to the manually created new users in ServiceNow.

Please find below the Business Rule details:
Table: User(sys_user)
Before Insert
Conditions:
Created by is not System

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

    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);
    }

    //Generate Notification
    gs.eventQueue("user.password", current, randomstring, current.email);
    //Set values in User record
    current.user_password.setDisplayValue(randomstring);
    current.password_needs_reset = true;
    current.update();
})(current, previous);

You can trigger the Notification and send the user's email and password in the parameters.
Note: You can even reuse the same code for reset the password.

Thanks & Regards,
Sailesh J

Comments
Naga23
Giga Expert

Hi Sailesh,

 

Good article, Could you please confirm on how can we generate the password such a way like it contains special characters also?

 

Regards!

Sailesh4
Giga Expert

Hi Dinesh,

 

Thanks for marking my article as Helpful.

 

You can add the required special characters, numbers in the variable "chars" and use the variable "string_length" for the length of the string required.

Just an FYI.. 

The passwords generated will be completely random. It is not 100% sure that we will get a word from it.

 

Regards,

Sailesh J

Naga23
Giga Expert

Hi Sailesh,

 

Thank you for the knowledge sharing. Keep doing good work like this.

 

Regards,

Dinesh

Dan_Berglin
Giga Expert

Hi,

You can use GlideSecureRandomUtil.getSecureRandomString(8) for creating random passwords.

Rozmin  Begum
Tera Contributor

i am getting the password but in encrypted form how can i decrypt and send it

 

jeffreyluto
Tera Contributor

I'm newer to SN, where does the code get added. I wanna try this to help speed up generating of onboarding many new users.

Version history
Last update:
‎10-16-2020 10:30 AM
Updated by: