How to change standard JS method in custom code

Changing Attach Datatype Clear Functionality (Dont delete from File if we click clear)

// Define the custom method
function custom_clear_attachment() {
    console.log("Custom clear_attachment method is loaded and executed");
    let me = this;

    if (this.frm) {
        me.parse_validate_and_set_in_model(null);
        me.refresh();
        me.frm.doc.docstatus == 1 ? me.frm.save("Update") : me.frm.save();
    } else {
        this.dataurl = null;
        this.fileobj = null;
        this.set_input(null);
        this.parse_validate_and_set_in_model(null);
        this.refresh();
    }

    // Optionally restore the original method here if needed
    restore_default_clear_attachment();
}

// Backup the original method
let original_clear_attachment = frappe.ui.form.ControlAttach.prototype.clear_attachment;

// Apply the custom method
function apply_custom_clear_attachment() {
    frappe.ui.form.ControlAttach.prototype.clear_attachment = custom_clear_attachment;
}

// Restore the original method
function restore_default_clear_attachment() {
    frappe.ui.form.ControlAttach.prototype.clear_attachment = original_clear_attachment;
}

	
//Applying in custom.js
onload: function(frm){
	// Define your custom clear_attachment method
	apply_custom_clear_attachment();
},

it is good approach for changing the core functionality in custom app?

1 Like

Very helpful! Keep it up.

1 Like