Refreshing child table duplicates rows

I have a function that updates rows in a child table, and a script that calls the function every time a field on the parent doc is changed. However, every time I change the field it appends the rows instead of updating them, so I end up with a table full of duplicate rows.

How do I prevent this from happening? In the function I have the following that performs the update:

for g in gl_entries:
    row = self.append('forex_accounts', {})
    g.account_name = g.account
    g.currency = g.account_currency
    g.current_balance = fmt_money(g.current_balance, 2, g.account_currency)
    g.current_gbp_balance = fmt_money(g.current_gbp_balance, 2, 'GBP')
    g.revalued_gbp_balance = fmt_money(g.revalued_gbp_balance, 2, 'GBP')
    g.difference = fmt_money(g.difference, 2, 'GBP')
    row.update(g)

And the script is simply:

frappe.ui.form.on('Forex Journal Entry', {
    posting_date: function(frm) {
        return frappe.call({
            method: "get_accounts",
            doc: frm.doc,
            callback: function(r, rt) {
                frm.refresh();
            }
        })
    }
});

Note that instead of frm.refresh() I have also tried:

frm.refresh_field("forex_accounts");
frm.refresh_fields();

with the same effect.

Hi,
If you are trying you add forex_accounts child table against each gl_entries it should be row.field_names instead of g and remove line row.update(g).
Thanks

Thanks for the reply. I’ve tried this and it’s still duplicating the rows if I use frm.refresh(). If I use frm.save() it doesn’t duplicate the rows but it also doesn’t update them.

EDIT: Using refresh_field("forex_accounts") doesn’t work either.

Fixed it by adding frm.clear_table("forex_accounts") to the javascript before calling the python function.

1 Like