Multiple apps cause custom scripts to be overwritten

Hi, I am not sure if this issue has been raised before. But this is an issue I’ve frequently encountered and need to always apply a workaround. It will be great if it’s fixed inside frappe code instead.

So Problem: If you install/develop 2 apps which has custom scripts on the same Doctype + same field, then only one of them will be executed. Reason is simple: the current recommended way of adding custom script now is like this:

cur_frm.cscript.custom_fieldname = function(doc) {
    console.log("some customize here");
}

So any app which is installed earlier will have their cur_frm.cscript.custom_fieldname overwritten by the later one. And i guess, from my perspective, that definitely shouldn’t be the default behavior.

Most of the time i would need to add this small script as a work around:

file: custom_scripts/common.js

addHandler = function(old_func, func) {
    return function() {
        if (typeof old_func === 'function') {
            old_func.apply(this, arguments);
        }
        func.apply(this, arguments);
    }
}

then hooks it to all the necessary apps:

doctype_js = {
    "Sales Order": ["custom_scripts/common.js", "custom_scripts/abcd.js"],
}

then use it now with confidence:

cur_frm.cscript.custom_fieldname = addHandler(cur_frm.cscript.custom_fieldname, function(doc) {
    console.log("This works");
}

This works, however, it is quite tedious because the process applies to any app we develop… Should we improve on this for frappe ?

1 Like

@nathan_dole is just to prevent that, that a new scripting model was introduced in frappe5

use

frappe.ui.form.on("{{doctype name goes here}}", {
    "{{ field or event goes here }}": function(frm, cdt, cdn){
       // your code goes here
    }
}) 

This new model manage the complexity of multi apps

3 Likes

cool! great to know that. Should also consider to change the documentation soon, i think