System built Auto Name works perfect, but I want some modifications in it.
I want a document name in the following manner:
Prefix/ABC/item/01
Where Item and numbers should be changed according to item.
Say item P has first version, so document name should come
Prefix/ABC/P/01
If it is for item Q it should come like
Prefix/ABC/Q/01
When there is a second version of item P it must come up with name Prefix/ABC/P/02 even it is created after Prefix/ABC/Q/01. Rather than creating Prefix/ABC/P/03
But very first concern, field:[fieldname] is not being fetched if it is within some alpha / numbers
For now it is fixed, if we can get multiple fields in document name using inbuilt Auto Name feature, should we add more than one fields like naming series (for Prefix/ABC like phrase), item name etc.
Ultimately document must be generated item wise in version manner.
@brillarescience Use this code to achieve the desired result , I hope you are setting the item name in item_name field and the value in item_code field , then set the auto name as field:item_code in your doctype
{
cur_frm.cscript.custom_item_name = compute;
function compute(doc, cdt, cdn)
{
//To check the first digit of the code
temp_code = "";
switch(doc.item_name)
{
default:
temp_code = doc.item_name.charAt(0);
}
//Get a list of all the codes with the same first digit
frappe.call({ method:"frappe.client.get_list",
args:{
doctype: "your_doc_name",
filters: {"item_code": ["like", temp_code + "%"]},
fields: ["name"],
limit_page_length: 500
},
callback: function(r) {
//Count the number of elements in the list (which will decide what code to try)
if (r.message) {
temp_code += zeroPad(r.message.length, 5);
console.log(temp_code );
}
else{
temp_code += "0000";
console.log(temp_code );
}
doc.item_code= temp_code;
refresh_field("item_code");
}});}
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}}