Try ERPNext Try Frappe Cloud Buy Support Partners Foundation

Generate a random string on employee custom field

I have a custom employee field called Misconduct Case Number that’s supposed to be extracted and used elsewhere outside ERPNext. The random string should be in the format [8 Alfanumeric charactors] [Date & Time] [Constant Organization Number] eg DX0FBN78 04200645 PTD0010045

I will appreciate any workaround

function randString(x){
    var s = "";
    var today = new Date();
    var date = String(today.getFullYear()).substring(2, 4)+''+(today.getMonth()+1);
    var time = today.getHours() + "" + today.getMinutes();
    var dateTime = date+time;
    var compNumber = " MSL12001749" ;
    while(s.length<x&&x>0){
        var r = Math.random();
        s+= (r<0.1?Math.floor(r*100):String.fromCharCode(Math.floor(r*26) + (r>0.5?97:65)));
    }
    return s.toUpperCase() + ' ' + dateTime + compNumber;
}

document.getElementById("foo").value = randString(40);

I am now able to get the last 2 parts of the string, the date, and company registration values but the random string is not being generated. And for some reason, there are no errors in the console. This is my custom script

frappe.ui.form.on('Employee', {
    validate: function (frm) {
        randString(frm);
    }
});

    var randString = function (frm) {
    var s = "";
    var x = "";
    var today = new Date();
    var date = String(today.getFullYear()).substring(2, 4) + '' + (today.getMonth() + 1);
    var time = today.getHours() + "" + today.getMinutes();
    var dateTime = date + time;
    var compNumber = " STR18001749";
    while (s.length < x && x > 0) {
        var r = Math.random();
        s += (r < 0.1 ? Math.floor(r * 100) : String.fromCharCode(Math.floor(r * 26) + (r > 0.5 ? 97 : 65)));
    }

    let  my_generated_string  = s.toUpperCase() + ' ' + dateTime + compNumber;
    frm.doc.misconduct  =  my_generated_string ;
    refresh_field('misconduct');
};

What am I missing?