Trouble with custom validation script

Im having trouble with a custom scrip used to both select the naming series based on the company and validate the naming series based on the company. I can get both part of the scripts to work independently but not together. when I combine both parts of the script I am unable to save.

I want the validate even tough it auto selects based on the company to ensure the user didn’t change the naming series manually before saving. I am still learning scripting so any help is greatly appreciated!

cur_frm.cscript.onload_post_render = function(doc,cdt,cdn){
    if(cur_frm.doc.company == "Cinema Hardware LLC"){
        cur_frm.set_value("naming_series","INV-.#####");
    }
    if(cur_frm.doc.company == "Cinema Hardware GmbH"){
        cur_frm.set_value("naming_series","RG-.#####");
    }    
};

frappe.ui.form.on("Sales Invoice", "validate", function(frm) {
    if (doc.company == "Cinema Hardware LLC") {
       if (frm.doc.naming_series != "INV-.#####"){
            msgprint("Please select correct naming series for company");
            Validate = false;
            
       }    
    }
    
    if (frm.doc.company == "Cinema Hardware GmbH") {
       if (frm.doc.naming_series != "RG-.#####"){
            msgprint("Please select correct naming series for company");
            Validate = false;

       }
    }   
     
});
  1. after set_value use frm.refresh_field(“naming_series”)
  2. use onchange function to set value and make naming series field read only than validation will be not required. on selection of company field your naming series field will automatically change the value.
1 Like

If you are auto fetching naming series, you can make this field as read only. Then there will be no need for validation.

Worked great! thank you!

can you share your script after correction?

I just used the same script but removed the validation and made the naming series field read only.