Code help: auto update cost center for child table items based on cost center of the sales invoice

Hi, I have written following script for sales invoice but it is somehow not updating cost centers of child table items when there is already a cost center selected. Basically it is not overwriting a previously selected value.

This is the code i am using:

frappe.ui.form.on(‘Task’, {
refresh: function(frm) {
for (var i in frm.doc.items){
frm.doc.items[i].cost_center=frm.doc.cost_center;
}
}
});

Can someone please guide what is the issue?

Hi @dextervarun,

Please apply it.

frappe.ui.form.on('Sales Invoice', {
    refresh: function(frm) {
        for (var i in frm.doc.items) {
            frm.doc.items[i].cost_center = frm.doc.cost_center;
        }
    }
});

Then reload and check it.

Thank You!

Works as always. Thanks @NCP