Client Script to add multiple items to a purchase order form

Hello,

I have created a client script to add a custom button to the items list ‘Add To Purchase Order’. I would like the function to add any selected items in the items list to a new purchase order form. Could someone please help me with this script.

Thank you

It would be challenging to create new purchase orders directly from the Items list because you would have to manage other required purchase order fields from there.
I would suggest you add a button to the Purchase Order Form like some buttons are already there.
image

That for, you have to apply the listview client script.

Please apply it.

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);
                });
            });
        });
    }
};
1 Like

Very useful thank you