Copy field value in Parent to Child Table

Hello Everyone,

I have created a custom field cost_center in Material Request form. I want to assign/update the same cost_center to cost centers of all items in child table of Material Request. I tried below to Client Scripts, and none of them seem to work.

Option#1

frappe.ui.form.on('Material Request',“refresh”, function(){
    for (var i =0; i < cur_frm.doc.items.length; i++){
        cur_frm.doc.items[i].cost_center = cur_frm.doc.cost_center
    }
    cur_frm.refresh_field(‘items’)
});

And, Option#2 -

frappe.ui.form.on('Material Request', {
  cost_center: function(frm) {
    $.each(frm.doc.items || [], function(i, d) {
        if(!d.cost_center) d.cost_center = frm.doc.cost_center;
    });
    refresh_field(“items”);
    }
}); 

frappe.ui.form.on('Material Request Item', {
	cost_center: function(frm, cdt, cdn) {
        if(!frm.doc.cost_center) {
            erpnext.utils.copy_value_in_all_rows(frm.doc, cdt, cdn, “items”, “cost_center”);
        }
    }
})

Hi @Nikhil_Rishi,

Option#1

It’s 100% work.
Please reload and check it.

Thanks.

Adding my other answer, first set a cost center after when select an item then shown item default cost center but you save it then automatically parent cost center set in child table cost center.

Thanks.