How To Fetch Data From ChildTable Field Into Parent Doctype Field

Dear Community,

I need your urgent help on how to fetch data/info from childtable field into a field on the parent doctype.

Parent doctype = Sales Invoice
Child table = Sales Taxes and Charges

Test Case:

  • I want to fetch $418.68 from Sales Taxes and Charges into the MarkUp Amount custom field in Sales Invoice
  • Also, I want to fetch $31.40 from Sales Taxes and Charges into the VAT Amount custom field in Sales Invoice

I have read other suggestions on the Forum but none has worked resolved my issue. The closest I got was this post Fetch the last row of child table and update field in main doc type - Frappe Framework - Frappe Forum. I applied the script suggested by @NCP. Still didn’t work.

Please help. Thank you.

Try this script

frappe.ui.form.on('Sales Invoice', {
    refresh: function(frm) {
        // Fetch values from child table and set them to custom fields
        var markupAmount = 0;
        var vatAmount = 0;

        // Loop through each row in the child table
        frm.doc.taxes.forEach(function(row) {
            // Check if the tax type matches and then set the values
            if (row.description === 'description 1') {
                markupAmount = row.tax_amount;
            } else if (row.description === 'description 2') {
                vatAmount = row.tax_amount;
            }
        });

        // Set the fetched values to the custom fields
        frm.set_value('mark_up_amount', markupAmount);
        frm.set_value('vat_amount', vatAmount);
    }
});

2 Likes

@rk_root
Thank you so very much. It worked.
Thank you. Thank you. Thank you.

1 Like