[Solve] Buttons to Manipulate Grid data

Hi All! I am working on a project and created this custom button. In order not to mess with the ERPNext existing code, I am calling my customized codes in my created app’s hooks.

Anyway, I have this button in Purchase Receipt that would set quantity to Purchase Receipt Items (Child). I have been looking buttons with the same functionality but can’t figure out how to code without using the “self” method since button will be called in hooks.

I tried doc = frappe.get_doc(“Purchase Receipt”, name) but will result to below error since data is not yet in the server side.

Any help is greatly appreciated. Thanks!

@creamdory share your script. Hard to say otherwise.

Hi @rmehta! Here’s what I’ve got. I am supposed to set received_qty to zero after pulling data from PO and let the user input whatever is receipt. Later on, I will be deleting lines with zero qty (items without receipt) with another button. So I am supposed to set received_qty using this code:

def set_qt_to_zero(name):

    doc = frappe.get_doc("Purchase Receipt", name)

    frappe.db.sql("""Update `tabPurchase Receipt Item` set received_qty = 0 where parent= %s""", 
                  doc.name)

Problem is, the record is still on the client side. That is why I am getting an error.

So basically, I’d like to know more about how to manipulate data on the client side. Thanks!

Solved this use case. Made custom script below for both “Set QTY to Zero” and “Delete Zero Lines”:


frappe.ui.form.on("Purchase Receipt", "set_qty_to_zero", function(frm, cdt, cdn) {
    var tbl = frm.doc.items || [];
    var i = tbl.length;
    while (i--) {
        if(frm.doc.items[i].qty != 0) {
            frm.doc.items[i].qty = 0
            frm.refresh_field("items")
        }
    }
});

frappe.ui.form.on("Purchase Receipt", "delete_zero_items", function(frm, cdt, cdn) {
    var tbl = frm.doc.items || [];
    var i = tbl.length;
    while (i--) {
        if(frm.doc.items[i].qty == 0) {
            cur_frm.get_field("items").grid.grid_rows[i].remove();
            frm.refresh_field("items")
        }
    }
});

3 Likes

is “grid_rows” a field name?