Hi All,I need to generate and set a random password for the selected user.Can anyone help me on this?Thanks

kirti0604
Kilo Contributor

Hi All,I need to generate and set a random password for the selected user.Can anyone help me on this?Thanks

11 REPLIES 11

helpful.


Thanks Mihir


HI Mihir


I am trying below code




function onAfter(current, previous) {


    //This function will be automatically called when this rule is processed.


  if (current.u_duration == '4') {


  gs.log("it is 4");


    // Set due date to 4 hours ahead of current time


    current.u_valid_till = gs.hoursAgo(-4);


  }


  if (current.u_duration == '6') {


  gs.log("it is 6");


    // Set due date to 6 hours ahead of current time


    current.u_valid_till = gs.hoursAgo(-6);


  }


  if (current.u_duration == '8') {


    // Set due date to 8 hours ahead of current time


    current.u_valid_till = gs.hoursAgo(-8);


  }


   


}


but it is not working as expected...


PS:checked the logs to ensure fields name have been correctly mentioned or not...logs are coming


To generate password with restrictions, you can use regex. Below ex. using regex condition for containing at least one capital letter, one small letter and one numerical.

var string_length = 6;
var patern=/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/;
var charList='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
do{
var password='';
for(var i=0;i<6;i++){
password+=charList.charAt(Math.random()*charList.length);
}
}while(!patern.test(password));

 

sushant06251
ServiceNow Employee
ServiceNow Employee

This example randomly generates a password from a base word and numbers. The base word is selected depending on the credential store. The code would be contained in the Script field of an extension script named SamplePasswordGenerator:



var SamplePasswordGenerator = Class.create();


SamplePasswordGenerator.prototype = {


  category: 'password_reset.extension.password_generator', // DO NOT REMOVE THIS LINE!



  /**********


    * Returns an auto-generated string password.


    * This sample randomly generates 4 digits to add to the password.


    *


    * @param params.credentialStoreId The sys_id of the target password reset credential store to generate


    *   a password for (table: pwd_cred_store)


    * @return An auto-generated string password


    **********/


  process: function(params) {


  var basePassword;



  var gr = new GlideRecord('pwd_cred_store');


  gr.addQuery('name', 'Local ServiceNow Instance');


  gr.query();


  if (gr.next()) {


  if (params.credentialStoreId == gr.getValue('sys_id'))


  basePassword = "Password";


  else


  basePassword = "Dorwssap";


  }


  return this.generateSimple(basePassword);


  },



  generateSimple : function(base) {


      var pwd = base;


      var numbers = '0123456789';


      var length = 4;



      for (var i = 0, n = numbers.length; i < length; i++) {


              pwd += numbers.charAt(Math.floor(Math.random() * n) +1);                


      }


      return pwd;


  },



  type: 'SamplePasswordGenerator'


};



http://wiki.servicenow.com/index.php?title=Password_Reset_Extension_Script_Includes#Password_Generat...


Hi Sushant,

 

Thank you for the script.

 

I have a similar kind of requirement. I have enabled the script from the password reset application.

 

I want to know how this functionality works.

 

Regards,

Sailesh J