Custom script close to working

Hi

It seems serial number for item when doing a purchase receipt is not working properly.
Automatic assignment is working, but if you have to enter the item serial number by hand, it
does not work.

I am trying to write my own script to handle this and I believe I am almost there but would like to ask for assistance for the last bit.

Use case:
Add a custom button in the “Items” table.
Select the row where the item requires a serial number.
Click the button.
New “Serial No” document is created and auto-populates item_code and document numbers on
the new Serial No doc
User enters the serial number for the item on the new Serial No doc
When saving the Serial No doc, newly created serial number is visible in items table of Pur-Receipt.

Here is my code…

rappe.ui.form.on('Purchase Receipt', {
    refresh: function(frm) {
        if (frm.doc.docstatus === 0) {
//            frappe.show_alert(frm.doc.supplier_name, 5);
            frm.fields_dict['items'].grid.add_custom_button(__('Create Serial Number'), function(doc) {
                var selected_item = frm.fields_dict['items'].grid.get_selected_children()[0]; // Get the selected row
                if (selected_item && !selected_item.serial_no) {
                    frappe.show_alert(selected_item.item_code, 5);
                    var newDoc = frappe.new_doc('Serial No');
                    newDoc.item_code = selected_item.item_code;
                    // Set any other fields as needed
                    frm.refresh();
//                    frappe.ui.form.save_single(newDoc);
                }
            });
        }
    }
});

My code created the a new Serial No document but the item_code field is not auto-populated.

The show_alerts are just for debugging.

I would like to ask for asisstance so that I can compete this.

Thank you

Hi @willspenc,

Please apply it.

frappe.ui.form.on('Purchase Receipt', {
    refresh: function(frm) {
        if (frm.doc.docstatus === 0) {
            frm.fields_dict['items'].grid.add_custom_button(__('Create Serial Number'), function(doc) {
                var selected_item = frm.fields_dict['items'].grid.get_selected_children()[0]; // Get the selected row
                if (selected_item && !selected_item.serial_no) {
                    var newDoc = frappe.model.get_new_doc('Serial No');
                    newDoc.item_code = selected_item.item_code;
                    newDoc.parenttype = 'Purchase Receipt';
                    newDoc.parentfield = 'serial_no';
                    newDoc.parent = selected_item.name;
                    frappe.ui.form.make_quick_entry('Serial No', null, frm, newDoc);
                }
            });
        }
    }
});

Then reload and check it.

Thank You!

1 Like

Thank you @NCP, it is working !!!

May I express my thanks for the help that you often extend. I put myself to task at the beginning of the year to learn to write my own custom scripts and I think I have made good progress. I need to acknowledge your contribution in that progress. Thank you

1 Like