Adding script for barcode to Purchase Order

Good day

adding client script fro a barcode to Purchase Order.

I have added a field called “PO Barcode” to my form
and added the script below. to populate the POBarcode field with the PO-nr.

frappe.ui.form.on('Purchase Order', {
    after_save: function(frm) {
        frm.set_value('po_barcode', frm.doc.name);
    }
});

I used the after_save trigger because the new PO-nr is only generated after I SAVE.
But now, after I save, the field is updated, putting the form back into UNSAVED state.

And so it just carries on. And I can’t use BEFORE SAVE because the POnr is not generaated then yet.

Then I added a custom button that the user can click. And if you click it after save, it
does indeed populate the barcode field with the PO nr BUT if the user clicks the button
BEFORE the PO nr is assigned it will load the New-Purchase Order -01

This is my button code …

frappe.ui.form.on('Purchase Order', {
    refresh: function(frm) {
        // Fetch sales order number from the work order doctype
        frm.add_custom_button(('Get Barcode'), function(){
            frm.set_value('po_barcode', frm.doc.name);
        },("Barcode"));        
    }
});

Can someone please assist me to get iether method working , I am stuck … thanks

Update

For those that is sitting with a similar problem , after a lot of googling, here is what I did.

This will create a custom button that is only available AFTER the first save and it fetches
the PO nr for the doc and places it in a custom barcode-field.

the
!frm.doc.__islocal
check is to check if it is not new

doc.docstatus
checks the status of the doc

Hope it helps someone

frappe.ui.form.on('Purchase Order', {
    refresh: function(frm) {
        if (!frm.doc.__islocal && frm.doc.docstatus === 0) {
            frm.add_custom_button('Get Barcode', function() {
                frm.set_value('po_barcode', frm.doc.name);
            });
        }
    }
});
1 Like