Customs script with if condition

any support on this code please,

Not working, i have no idea how to fix it,

cur_frm.cscript.onload_post_render = function(frm,dt,dn){

    if(doc.company == 'Company 1'){
        cur_frm.set_value("naming_series","COM1SO")
    }else if(doc.company == 'Company 2'){
        cur_frm.set_value("naming_series","COM2SO")
    }else if(doc.company == 'Company 3'){
        cur_frm.set_value("naming_series","COM3SO")
    }

}
1 Like

use frm.doc.company instead of doc.company

1 Like

Probably would be best to drop cur_frm, right? I’m pretty sure it’s not recommended. Not sure if onload_post_render works with the following, but I guess try it?

frappe.ui.form.on("DocType", {
    onload: function(frm) {
        if(frm.doc.company == 'Company 1') {
            frm.set_value("naming_series","COM1SO");
        } else if(frm.doc.company == 'Company 2') {
            frm.set_value("naming_series","COM2SO");
        } else if(frm.doc.company == 'Company 3') {
            frm.set_value("naming_series","COM3SO");
        }
    }
});

Alternatively, you could use the switch function, which some prefer for readability.

frappe.ui.form.on("DocType", {
    onload: function(frm) {
        switch(frm.doc.company) {
            case 'Company 1':
                frm.set_value("naming_series","COM1S0");
                break;
            case 'Company 2':
                frm.set_value("naming_series","COM2S0");
                break;
            case 'Company 3':
                frm.set_value("naming_series","COM3S0"); 
        }
    }
});
2 Likes

thank you but i add that and still have some errors

i tried both of the scripts but nothing happen any suggestion where could i went wrong?

Did you change "DocType" in frappe.ui.form.on("DocType", { to the doctype that you are running the script in?

oh ok thank you didnt change there
but can you try i am trying it in doctype payment entry and it doesnt work any issue with this doctype but for Sales invoice works fine

@Dany_Carvalheiro
Even i am also persisting same problem, have you got any solution for this?

thanks.

Trying on which doctype?

Payment Entry…

frappe.ui.form.on("Payment Entry", {
    onload: function(frm) {
        switch(frm.doc.company) {
            case 'ABC':
                frm.set_value("naming_series","ABC-PE-");
                break;
            case 'XYZ':
                frm.set_value("naming_series","XYZ-PE-");
                break;
        }
    }
});

In my use case i have different valuse… i tried both switch and if else case not working. In payment entry doctype naming series are:
Receipt-18-19-
Payment-18-19-
Contra-18-19- And Payment Type are
Receive
pay
internal Transfer. Both field in same doctype, i,e Payment entry. If i select Receipt-18-19- in naming series, it should reflect Receive in Payment type. I tried below custom script

frappe.ui.form.on(“Payment Entry”, {
onload: function(frm) {
if(frm.doc.naming_series == ‘Receipt-18-19-’) {
frm.set_value(“payment_type”,“Receive”);
} else if(frm.doc.naming_series == ‘Payment-18-19-’) {
frm.set_value(“payment_type”,“Pay”);
} else if(frm.doc.naming_series == ‘Contra-18-19-’) {
frm.set_value(“payment_type”,“Internal Transfer”);
}
}
});

frappe.ui.form.on("Payment Entry", {
naming_series: function(frm) {
if(frm.doc.naming_series == "Receipt-18-19-") {
frm.set_value("payment_type","Receive");
} else if(frm.doc.naming_series == "Payment-18-19-") {
frm.set_value("payment_type","Pay");
} else if(frm.doc.naming_series == "Contra-18-19-") {
frm.set_value("payment_type","Internal Transfer");
}
}
})
1 Like

Thank you. I will check it