How to preload data in child table for web from

Hi, i am currently creating a survey using the web from and a doctype .

I need to pre set data in the child table row for users to leave rating.

@Vasanth_Iyyanar_Rang Welcome to ERPNext community.

You can achieve what you want by creating a Client Script, set the doctype as Web Form and the code will be like the following.

Assuming that the child table fieldname is “items”

frappe.ui.form.on("Web Form", {
    refresh: function(frm) {
        // check if child table fieldname exists in the doctype or not
        if (!frm.doc.items) return;
        frm.add_child('items', {
            item_code: 'Tennis Racket',
            qty: 2
        });
        // finally refresh the child table
        frm.refresh_field('items');
    }
});

Dear @kid1194 , Thank you so much for your help ,i did create a client script and chose web form as doctype.

for some reason its not working .

frappe.web_form.set_df_property([fieldname], [property], [value]);

will this work for child table ?

@Vasanth_Iyyanar_Rang Sorry, the code I posted is not ment to work with Web Forms.
What you want to do can’t be done directly, so the following code is based on frappe source code and it is supposed to be a workaround. i hope it works for you.

frappe.web_form.after_load = function() {
        let me = frappe.web_form;
        // check if the web form is what you are looking for
        if (me.doc.doctype !== "Survey") return;
        // check if child table fieldname exists in the doctype or not
        if (!me.has_field('items')) return;
        let field = me.get_field('items');
        // check if field is table
        if (!field.grid) return;
        let row = field.grid.add_new_row(null, null, true, null, true),
        grid_rows = field.grid.grid_rows,
        row_name = grid_rows[grid_rows.length - 1].doc.name;
        frappe.model.set_value(field.grid.doctype, row_name, "fieldname", "value");
    }
};
1 Like

frappe.ready(function() {
frappe.web_form.after_load = function() {

    let me = frappe.web_form;
	console.log(me);

    // check if the web form is what you are looking for

    if (me.doc.doc_type !== "Unit Food Survey") return;

    // check if child table fieldname exists in the doctype or not

    if (!me.has_field('use_food_type_restaurant')) return;

    let field = me.get_field('use_food_type_restaurant');

    // check if field is table

    if (!field.grid) return;

    let row = field.grid.add_new_row(null, null, true, null, true),

    grid_rows = field.grid.grid_rows,

    row_name = grid_rows[grid_rows.length - 1].doc.name;

    frappe.model.set_value(field.grid.doctype, row_name, "produces_name", "Milk");

}

})

1 Like

Dear One , This are the corrected field names.but still its not functioning as expected.

DId you get solution for this…??

Yes, i managed get it to work

@Vasanth_Iyyanar_Rang how did you succeed…?