在 ERPNext 中的员工自定义字段上生成随机字符串

Generate a random string on employee custom field in ERPNext

我有一个名为“不当行为案例编号”的自定义员工字段,该字段应该被提取并在 ERPNext 之外的其他地方使用。随机字符串的格式应为 [8 个字母数字字符] [日期和时间] [常量组织编号] 例如 DX0FBN78 04200645 PTD0010045

出于某种原因,我无法使用以下自定义脚本生成随机字符串,并且控制台中没有错误。

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

嗯,首先,x 不是一个数字,不会改变,永远不会满足 x > 0

不确定 ERPNext 支持哪种 JS,但这应该可以:

var pool = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
var compNumber = " STR18001749";

var randString = function(frm) {
  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;

  while (s.length < 8) {
    var i = Math.floor(Math.random() * pool.length);
    s += pool[i];
  }

  frm.doc.misconduct = s.toUpperCase() + ' ' + dateTime + compNumber;
  // refresh_field('misconduct'); // commented out so the snippet runs
};

x = {doc: {}};
randString(x);
console.log(x);