Solved it.
-
added a field in doctype called "print_format_selector
-
created custom button to call print function and set it to load on page refresh
function custom_print(frm){ cur_frm.add_custom_button(__("Button Name 1"), function() { cur_frm.set_value("print_format_selector","format_1") cur_frm.print_doc(); }); cur_frm.add_custom_button(__("Button Name 2"), function() { cur_frm.set_value("print_format_selector","format_2") cur_frm.print_doc(); }); } -
Overrided the default print format fetching function and added an if condition to check if there exists any value in that field and if there exists any, return that value as the selected print format
$.extend(frappe.meta, { get_print_formats: function(doctype) { var print_format_list = ["Standard"]; var default_print_format = locals.DocType[doctype].default_print_format; let enable_raw_printing = frappe.model.get_doc(":Print Settings", "Print Settings").enable_raw_printing; var print_formats = frappe.get_list("Print Format", {doc_type: doctype}) .sort(function(a, b) { return (a > b) ? 1 : -1; }); $.each(print_formats, function(i, d) { if ( !in_list(print_format_list, d.name) && d.print_format_type !== 'JS' && (cint(enable_raw_printing) || !d.raw_printing) ) { print_format_list.push(d.name); } }); if(default_print_format && default_print_format != "Standard") { var index = print_format_list.indexOf(default_print_format); print_format_list.splice(index, 1).sort(); print_format_list.unshift(default_print_format); } if(cur_frm.doc.print_format_selector){ //newly added if condition var print_format = [cur_frm.doc.print_format_selector] return print_format } else{ return print_format_list; } }, });
To override, i created a new file in apps/appname/appname/public/js/over_rided_func.js and included that file in build.json and also in hooks.py did a bench build once, then bench --site sitename migrate, bench clear-cache, bench restart
in build.json
{
"js/over_rided_func.js": [
"public/js/over_rided_func.js"
]
}
in hooks.py
doctype_js = {"MyDoctype" : "public/js/over_rided_func.js"}