Custom Script : Part 2

Hi

I am asking for some advise. I have completed my custom script which seems to work. But I would like to improve it.

Goal…
In purchase receipt, if the item that is received has a serial number that is generated automatically ,
then Purchase receipt works. If the serial number field in the item master is blank i.e. serial
number to be supplied by the user, purchase receipt does not work.

I decided to create a little custom scripty for this. The process is as follows…
There is now a custom button in the items table. The row of the item that the user wants to create
a serial number is selected and the user selects the “Create serial number” button. This opens a
new serial no doc and auto fills some fields. The user enters the serial number and saves.

There is also an added field in the items table of purchase receipt called “custom_serial_number”.
This is of type LINK that links to serial no doctype. And after the user has created the serial number
as described above, he/she can then select that serial number in that field. The filters has been extended to make available only the created serial number.

This process can work.

I was wondering if there is not a way that, after the user has created the serial number ( using the
custom button) that the field in the items table ( custom_serial_number) can be auto-loaded ??

I hope I have explained clearly.

Does someone perhaps have some advise for me to implement such an improvement ?

Here is code

frappe.ui.form.on('Purchase Receipt', {
	refresh(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.purchase_document_type = 'Purchase Receipt';
                    newDoc.purchase_document_no = frm.doc.name;
                    newDoc.parenttype = 'Purchase Receipt';
                    newDoc.parentfield = 'serial_no';
                    newDoc.parent = selected_item.name;
                    frappe.ui.form.make_quick_entry('Serial No', null, frm, newDoc);
                }
            });
        }	    
		cur_frm.fields_dict["items"].grid.get_field("custom_serial_number").get_query = function(doc, cdt, cdn) {
			var row = locals[cdt][cdn];
        	return {
		            filters: {
		                "status": 'Inactive', 
		                "item_code": row.item_code,
		                "purchase_document_type": 'Purchase Receipt',
		                "purchase_document_no": frm.doc.name
		            }
            };
        };		
	}
});

frappe.ui.form.on('Purchase Receipt Item', {
	custom_serial_number: function(frm, cdt, cdn) {
		var row = locals[cdt][cdn];
		row.serial_no = row.custom_serial_number; 
		refresh_field("items"); 
	}
});

Thank you

Hi @willspenc,

Hmm :thinking:,

Please try it.

frappe.ui.form.on('Purchase Receipt', {
    refresh(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.purchase_document_type = 'Purchase Receipt';
                    newDoc.purchase_document_no = frm.doc.name;
                    newDoc.parenttype = 'Purchase Receipt';
                    newDoc.parentfield = 'serial_no';
                    newDoc.parent = selected_item.name;
                    frappe.ui.form.make_quick_entry('Serial No', function(doc) {
                        // After the serial number is created, set it in the custom_serial_number field
                        frm.fields_dict['items'].grid.get_field('custom_serial_number').get_query = function(doc, cdt, cdn) {
                            return {
                                filters: {
                                    "status": 'Inactive', 
                                    "item_code": selected_item.item_code,
                                    "purchase_document_type": 'Purchase Receipt',
                                    "purchase_document_no": frm.doc.name
                                }
                            };
                        };
                        frappe.call({
                            method: 'frappe.client.get_value',
                            args: {
                                doctype: 'Serial No',
                                filters: {
                                    "name": doc.name
                                },
                                fieldname: 'name'
                            },
                            callback: function(r) {
                                if (r.message) {
                                    selected_item.custom_serial_number = r.message.name;
                                    frm.fields_dict['items'].grid.refresh();
                                }
                            }
                        });
                    }, frm, newDoc);
                }
            });
        }
    }
});

Please check your field name and doctype name and set it in script your according.

Thank You!

Thank you @NCP for your assistance

Apologies for the late response. I had a crisis to deal with.

I entered you code but after I saved the new Serial No document and return to the Purchase receipt,
the Serial Number field does not show the newly created Serial number.

I put in a few show_alert prom,pt to trace the code and I get “one” displayed and “two”, but the
show_alert that I put in to show the newly created serial number does not pop up on the screen.

Here is the code with the “show_alerts”.

frappe.ui.form.on('Purchase Receipt', {
    refresh(frm) {
        if (frm.doc.docstatus === 0) {
            frappe.show_alert("one", 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) {
                    var newDoc = frappe.model.get_new_doc('Serial No');
                    newDoc.item_code = selected_item.item_code;
                    newDoc.purchase_document_type = 'Purchase Receipt';
                    newDoc.purchase_document_no = frm.doc.name;
                    newDoc.parenttype = 'Purchase Receipt';
                    newDoc.parentfield = 'serial_no';
                    newDoc.parent = selected_item.name;
                    frappe.show_alert("two", 5);
                    frappe.ui.form.make_quick_entry('Serial No', function(doc) {
                        // After the serial number is created, set it in the custom_serial_number field
		cur_frm.fields_dict["items"].grid.get_field("custom_serial_number").get_query = function(doc, cdt, cdn) {
			var row = locals[cdt][cdn];
			frappe.show_alert("three", 5);
        	return {
		            filters: {
		                "status": 'Inactive', // Replace "link_fieldname" with the actual fieldname used to link to the "Serial No" doctype
		                "item_code": row.item_code,
		                "purchase_document_type": 'Purchase Receipt',
		                "purchase_document_no": frm.doc.name
		            }
            };
        };
                        frappe.call({
                            method: 'frappe.client.get_value',
                            args: {
                                doctype: 'Serial No',
                                filters: {
                                    "purchase_document_no": doc.name
                                },
                                fieldname: 'name'
                            },
                            callback: function(r) {
                                if (r.message) {
                                    selected_item.custom_serial_number = r.message.name;
                                    frappe.show_alert(r.message.name, 5);
                                    frm.fields_dict['items'].grid.refresh();
                                }
                            }
                        });
                    }, frm, newDoc);
                }
            });
        }
    }
});