Add child row when creating new document

Hi guys, can anybody help me? How to add child table rows upon trigger new document?

Doctype: Sales Expense Liquidation
Table field name: header
Table: Sales Liquidation Header

frappe.ui.form.on('Sales Expense Liquidation', {
	refresh(frm) {
		frappe.ui.form.on('Sales Expense Liquidation', {
		    new: function(frm) {
                        
                        frm.add_child("header").particulars = "Date";
                        frm.add_child("header").particulars = "Travel From";
                        frm.add_child("header").particulars = "Travel To";
		        
            }
		});
	}
});

Hi, try this

// Attach a handler to the 'Sales Expense Liquidation' form
frappe.ui.form.on('Sales Expense Liquidation', {
    // Event triggered when the form is refreshed
    refresh(frm) {
        // Check if the form is for a new document
        if (frm.is_new()) {
            // Add the first row to the 'header' child table and set its value
            let row1 = frm.add_child("header");
            row1.particulars = "Date";

            // Add the second row to the 'header' child table and set its value
            let row2 = frm.add_child("header");
            row2.particulars = "Travel From";

            // Add the third row to the 'header' child table and set its value
            let row3 = frm.add_child("header");
            row3.particulars = "Travel To";

            // Refresh the child table field to reflect the added rows in the UI
            frm.refresh_field("header");
        }
    }
});

This is an example. check that the fields are correct, etc and modify the code to your liking

Worked perfectly :smile: Thank you so much!!!

1 Like