Auto populate child table in purchase receipt

So I was able to try this and get the desired effect, however I did this on a button click:

Python:

import frappe

@frappe.whitelist()
def get_items_supplied(supplier):
    items = frappe.get_all(
        "Item Supplier",
        {"supplier":supplier},"parent",
        as_dict=1)
    return items

Javascript

fetch_supplied_items is my button field.

frappe.ui.form.on("Purchase Receipt",{
    "fetch_supplied_items": function(frm, cdt, cdn){
        frappe.call({
            method:"randomcode.randomcode.scripts.validations.get_items_supplied",
            args:{
                supplier:frm.doc.supplier
            },
            callback:function(r){
                if (r.message){
                    //setting the table empty, erpnext now has one row by default
                    if(frm.doc.items){
                        frm.set_value("items",[])
                    }
                    $.each(r.message, function(i, item) {
                        var item_row = frm.add_child("items")
                        console.log(item)
                        item_row.item_code = item.parent
					});
                }
            }
        })
        frm.refresh()
    }
})