How to auto increment a particular field in a doctype?

Hi All ,

Could anyone just tell me how to increment a particular field , when enter into a doctype . I don want something like the naming series.

Thanks in advance

1 Like

@srinivasragav have a look at

http://frappe.github.io/erpnext/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples/

1 Like

Hi @JoEz , Thank you your swift response , the contents in the above topic was not relevant to what I am searching ? Could you guide me with some other link ?

Thanks in advance

1 Like

@srinivasragav can u explain exactly what u need?

2 Likes

@JoEz . Thanks a ton for quick response actually I want to generate a code for customer , the customer_code should be auto generated upon entering the customer name , the logic is `First letter should be from the customer name , the second letter is fixed it is ‘‘C’’ , the next four numbers should go increasing from 0001 to 9999 . I created a custom script by which I was able to achieve the first two things and I am struck up with the auto increment value ?

My script:
{
cur_frm.cscript.custom_customer_name = compute;
cur_frm.cscript.custom_tax_id = compute;

function compute(doc, cdt, cdn){
    // clear customer_code (name is from customer_code)
    doc.customer_code = "";

    // first 2 characters based on customer_name
    switch(doc.customer_name) {
        case "Test A":
            doc.customer_code = "TA";
            break;
        case "Test B":
            doc.customer_code = "TB";
            break;
        default:
            doc.customer_code = doc.customer_name.charAt(0);
    }

    // add next 2 characters based on customer_type
    switch(doc.tax_id) {
                 default:
            doc.customer_code += "C0001";
  }


refresh_field("customer_code");
}
}

Could you please guide me on how to increment a field from 0000 to 9999 , when adding customers ?

Thanks in advance

1 Like

@srinivasragav try using

doc.customer_code += "C.####";

1 Like

Many thanks for quick response , I did that already it just prints C.#### , since it is enclosed within “”

1 Like

Is there a way to count the number of documents in a doctype and put the value in one field ?

1 Like

Based on our private messages, we have developed the code to achieve what was desired here. I’m sharing it for others to be able to use:

{

cur_frm.cscript.custom_customer_name = compute;
function compute(doc, cdt, cdn)
{
    //Find out the first two digits of the code
	temp_code = "";
	switch(doc.customer_name) 
	{
		default:
		temp_code = doc.customer_name.charAt(0);
		temp_code += "C";
	}
	//Get a list of all the exisiting codes with the same first two digits
	frappe.call({ method:"frappe.client.get_list",
		args:{
			doctype: "Customer",
			filters: {"customer_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 customer code to try)
			if (r.message) {
				temp_code += zeroPad(r.message.length, 5);
							console.log(temp_code );
			}
					else{
				temp_code += "00000";
							console.log(temp_code );
			}
			// You probably want to add a section of code here that checks to see if the new code is an element of the list, 
			// and increment the code if that customer code already exists (this only comes up if a customer is deleted)
			// Otherwise, you could create multiple customers with the same code.
			//Submit the customer code and refresh the field
			doc.customer_code= temp_code;
			refresh_field("customer_code");
		}
	});
}	
function zeroPad(num, places) {
	var zero = places - num.toString().length + 1;
	return Array(+(zero > 0 && zero)).join("0") + num;
}
}
6 Likes