This ⬆️ is the reference to that script that I found on the Frappe Forums.
To do this, first, you have to override the doctype.js file. Here is the reference link for that. Okay, wait, I know you are lazy; I will copy and paste some code for you 🤭, if you want to read about it, you can visit the link.
You can override/extend Standard Form Scripts by using the doctype_js hook.
app/hooks.py
doctype_js = {
"ToDo": "public/js/todo.js",
}
app/public/js/todo.js
frappe.ui.form.on("Todo", {
refresh:function(frm) {
frm.trigger("my_custom_code");
},
my_custom_code:function(frm){
console.log(frm.doc.name)
}
});
frappe.listview_settings['Item'] = {
onload: function(listview) {
listview.page.add_inner_button('Create Purchase Order', function() {
let selected_item_names = listview.get_checked_items(true);
if (selected_item_names.length === 0) {
frappe.msgprint(__('Please select at least one item.'));
return;
}
frappe.new_doc('Purchase Order', {}, function(purchase_order) {
frappe.model.clear_table(purchase_order, 'items');
selected_item_names.forEach(item_name => {
let child_row = frappe.model.add_child(purchase_order, 'items');
frappe.model.set_value(child_row.doctype, child_row.name, 'item_code', item_name);
});
});
});
}
};
This code is provided by NCP
Button Creation:
We add a button labeled "Create Purchase Order" on the ListView page using the listview.page.add_inner_button function.
listview.page.add_inner_button('Create Purchase Order', function() {
Item Selection:
We use the listview.get_checked_items(true) function to gather the items that the user has selected (checked) from the list. The true parameter ensures that we only get the names of the selected items, which we store in the selected_item_names variable.
let selected_item_names = listview.get_checked_items(true);
Validation:
Before proceeding, we check if the user has selected any items. If no items are selected (i.e., the length of selected_item_names is zero), we show a message asking the user to select at least one item using frappe.msgprint.
if (selected_item_names.length === 0) {
frappe.msgprint(__('Please select at least one item.'));
return;
}
Creating the Purchase Order:
If the validation passes (i.e., at least one item is selected), we create a new Purchase Order document using frappe.new_doc('Purchase Order',). This opens the form for the new Purchase Order.
frappe.new_doc('Purchase Order', {}, function(purchase_order) {
Table Management:
Before adding the selected items to the Purchase Order, we clear the items table using frappe.model.clear_table. This ensures that the table is empty before we start adding the selected items, avoiding any duplication.
frappe.model.clear_table(purchase_order, 'items');