Need help with code generation

Hi All ,

I am trying to generate a customer code based on some logic , the logic is

First letter should be from the customer name , the second letter is fixed it is ‘‘C’’ , the next two numbers should go increasing from 01 to 99 . Please someone guide me with an example ,

@srinivasragav have a loot at:

https://frappe.github.io/erpnext/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples/generate-item-code-based-on-custom-logic.html

Hi , tried the below script but it is not working , could you guide me , where I am going wrong ?

cur_frm.cscript.custom_validate = function(doc) {
// clear item_code (name is from customer_code)
doc.customer_code = “”;

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

// add next 2 characters based on customer_name
switch(doc.customer_name {
    case "Brand A":
        doc.customer_code += "BA";
        break;
    case "Brand B":
        doc.customer_code += "BB";
        break;
    default:
        doc.customer_code += "BX";
}

}

Try using the console log to see if you are getting errors.

1 Like

Thanks a lot for taking time to solve my issue , Sir I saw in the console no errors , but the script is not working . So i did one thing to ensure , whether is there any problem or bug with script, I copied this script and checked ,

{
function compute(doc, cdt, cdn){
    if (doc.customer_name){ 
doc.customer_code = doc.customer_name;
refresh_field("customer_code");
    }}
cur_frm.cscript.custom_customer_name = compute;

}

This works like a charm , but I dont know where I am going wrong with the code generation script ?Could you guide me with an example sir ?
Thanks in advance

It could be because you aren’t running refresh_field("customer_code"); in your script

1 Like

Sir , thanks a lot for swift response , i even tried inserting refresh_field("customer_code"); in my script , but I could not see the script working . Please help

Can you explain how you are using this? What doctype is this for? Where is the script saved (which doctype)? What is your workflow that is not working?

1 Like

Sir I am thankful to you , for your swift reply , guidance and help . You are a customization star .
I am using this script to automatically generate a code for the new customer when the user enters the customer name , actually what i want is to generate a code like this ,
First letter should be from the customer name , the second letter is fixed it is ''C'' , the next two numbers should go increasing from 01 to 99
so I am working with few examples to get started with so I can then go on to the next level , i copied it under the customer custom script location . This is for customer doctype . Can you please guide me with this .

Thanks in advance

With the below script I am able to print the first character of the customer name and the fixed letter “C” , but i want the next 4 digits to auto increment , for now it prints 0001 any help on this ? Please

{
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) {
        case "Type A":
            doc.customer_code += "BA";
            break;
        case "Type B":
            doc.customer_code += "BB";
            break;
         default:
            doc.customer_code += "C0001";
    }
refresh_field("customer_code");
}
}