How to fetch the value from child table to another child table in Same Doctype

Hi all,

I need to fetch the value of avg_rate from Sales Invoice item table and paste it to Packing List table in Sales Invoice document.

For that my script is here and working fine that i am getting a value in console, but don’t know how to set value to the rate field in Packed Items Table.

Note that avg_rate in Sales Invoice Item and rate in Packed Items table are custom fields.

frappe.ui.form.on("Sales Invoice", "validate", function(frm) {
    console.log("frm.", frm);
    var packeditems = frm.doc.packed_items;
    var items = frm.doc.items;
    for (i = 0; i < packeditems.length; i++) {
        if (packeditems) {
            console.log("packeditems", packeditems);
            for (j = 0; j < items.length; j++) {
                if (packeditems[i].parent_item == items[j].item_code) {
                    var rate = items[j].avg_rate;
                    console.log("avg_rate", rate);

                }
            }
        }
    }
});

1 Like

I’m confused about the field you are trying to set; you want all the packed items to show the same averaged rate? You’ll need to iterate through the packed_items list to do that.

for(let i in packed_items){
    packed_items[i].rate = avg_rate
}
frm.refresh_field("packed_items")

@tmatteson In India if the item’s rate (i.e single row) is more than 1000 means, the GST tax will be higher. So to reduce the user entry time using Product Bundle feature and showing packing list with avg rate in Print format alone. Thanks

That’s interesting, thanks for the context. If this works out for you, please post your whole solution here (it’s giving back to the community).

@tmatteson the script is not working, we have to fetch the avg_rate from item_code row in Sales Invoice Item and paste it in parent_item row in Packed Items, only if both item_code and parent_item matches. Anyone please help on it.

Thanks.

Short version: you shouldn’t be doing this as a custom script, it should have a dedicated python method and have it called when you load the packing list. Are you self hosted or on erpnext.com?

@tmatteson Not self hosted, erpnext.com. Noted.

Thanks.

Since you’re on the cloud, it will have to be all custom script and the “right way” to do it is out the window. It’s possible, but will take some more engineering and you can only use existing server methods. Have a look through frappe.client to see which of those makes the most sense for your use case. You’re going to have to get creative with the filters.

try this i set both field same value you can set according your requirements:

frappe.ui.form.on("Sales Invoice", "validate", function(frm) {
    console.log("frm.", frm);
    var packeditems = frm.doc.packed_items;
    var items = frm.doc.items;
    for (i = 0; i < packeditems.length; i++) {
        if (packeditems) {
            console.log("packeditems", packeditems);
            for (j = 0; j < items.length; j++) {
                if (packeditems[i].parent_item == items[j].item_code) {
                    var rate = items[j].avg_rate;
                    frappe.model.set_value(items[j].doctype,items[j].name,"avg_rate",rate)
                    frappe.model.set_value(items[i].doctype,items[i].name,"rate",rate)
                    console.log("avg_rate", rate);

                }
            }
        }
    }
});

@Maheshwari_Bhavesh No, it doesn’t work with below

frappe.ui.form.on(“Sales Invoice”, “validate”, function(frm){
console.log(“frm.”,frm);
var packeditems = frm.doc.packed_items;
var items = frm.doc.items;
for (i = 0; i < packeditems.length; i++) {
if (packeditems) {
console.log(“packeditems”,packeditems);
for (j = 0; j < items.length; j++){
if(packeditems[i].parent_item == items[j].item_code){
var rate = items[j].avg_rate;
console.log(“avg_rate”,rate);
frappe.model.set_value(packed_items[i].doctype,packed_items[i].name,“rate”,rate)
}
}
}
}
});

hi all
could any one tell us how to make this using custom Button as trigger instead of ```
“validate”
the button name is trk_calc
so i want every thing in the Items child table to be copied to items_track child table within purchase order Doctype when this trk_calc button clicked