1

我有一个名为 Misconduct Case Number 的自定义员工字段,它应该被提取并在 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');
};
4

1 回答 1

1

好吧,首先,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);

于 2021-04-23T03:28:02.353 回答