Add Child Table rows based on Parent Doctype field

i have a scenario;
i have field with number of item for e.g if is 5 then add 5 rows in the child table simple but after lots of work i could not do that .

Please check the syntax:

frappe.ui.form.on('DocType', {
    no_of_rows: function(frm) {
        let no_of_rows = frm.doc.no_of_rows;
        let item_code = "Item Name";

        if (!no_of_rows || no_of_rows <= 0) {
            frappe.msgprint(__('Please enter a valid number of rows.'));
            return;
        }

        frm.clear_table("table_name");

        for (let i = 0; i < no_of_rows; i++) {
            let new_row = frm.add_child("table_name");
            frappe.model.set_value(new_row.doctype, new_row.name, 'item_code', item_code);
        }
        frm.refresh_field("table_name");
    }
});

Please set the doctype, fieldname, table_name, and table_field_name in the script according to the scenario.

frappe.ui.form.on(‘Courier Gate Pass’, {
no_of_rows: function(frm) {
let no_of_packages = frm.doc.no_of_packages; //field at which total number of child which had to been added accordingly
let dimension = “33 X 26 X 20”; link field which is in child table that is not required i just want empty rows will be added in the child table

    if (!no_of_packages || no_of_packages <= 0) {
        frappe.msgprint(__('Please enter a valid number of rows.'));
        return;
    }

    frm.clear_table("dimensions");

    for (let i = 0; i < no_of_rows; i++) {
        let new_row = frm.add_child("dimensions");
        frappe.model.set_value(new_row.dimensions //child table name, new_row.dimensions // child table fieldname, 'dimension' //item to add, dimension);
    }
    frm.refresh_field("dimensions");
}

});

please check i am doing every right

This line is wrong. first set the static value and test it, if it is worked then try to set the dynamic scenario.

what will be in this line new_row.doctype, new_row.name

new_row.doctype // child table name for e.g new_row_dimension
new_row.name // child table field name for e.g new_row.dimensions
dimensions // field name of the child table
dimension // data pass which had to be send in the child table

Please check the code.

frappe.ui.form.on('Sales Order', {
    customer: function(frm) {
        let no_of_rows = 5;
        let item_code = "Finish Item 1";

        if (!no_of_rows || no_of_rows <= 0) {
            frappe.msgprint(__('Please enter a valid number of rows.'));
            return;
        }

        frm.clear_table("items");

        for (let i = 0; i < no_of_rows; i++) {
            let new_row = frm.add_child("items");
            frappe.model.set_value(new_row.doctype, new_row.name, 'item_code', item_code);
        }
        frm.refresh_field("items");
    }
});

When customer select the add the 5 item.

frappe.ui.form.on(‘Courier Gate Pass’, {
no_of_packages: function(frm) {
frappe.msgprint(“Hello”)
let no_of_packages = frm.doc.no_of_packages;
frappe.msgprint(no_of_packages)
let dimension = “33 X 26 X 20”;

    if (!no_of_packages || no_of_packages <= 0) {
        frappe.msgprint(__('Please enter a valid number of rows.'));
         frappe.msgprint(no_of_packages)
        return;
    }

    frm.clear_table("dimensions");

    for (let i = 0; i < no_of_packages; i++) {
        let new_row = frm.add_child("dimensions");
        frappe.model.set_value(new_row.dimensions, new_row.dimensions, 'dimension', dimension);
        frappe.msgprint(new_row)
    }
    frm.refresh_field("dimensions");
}

});

i write this code
i get hello and then blank promt

No need to change.

frappe.ui.form.on(‘Courier Gate Pass’, {
no_of_packages: function(frm) {
frappe.msgprint(“Hello”)
let no_of_packages = frm.doc.no_of_packages;
frappe.msgprint(no_of_packages)
let dimension = “33 X 26 X 20”;

    if (!no_of_packages || no_of_packages <= 0) {
        frappe.msgprint(__('Please enter a valid number of rows.'));
         frappe.msgprint(no_of_packages)
        return;
    }

    frm.clear_table("dimensions");

    for (let i = 0; i < no_of_packages; i++) {
        let new_row = frm.add_child("dimensions");
        frappe.model.set_value(new_row.doctype, new_row.name, 'dimension', dimension);
        frappe.msgprint(new_row)
    }
    frm.refresh_field("dimensions");
}

});


no child table are been added

I think, you don’t understand the code.

Please check it code and apply it to the sales order and test it. Also set the static Item.


successfully adding in this

worked for testing on sales order but not worked as per the required scenario @NCP

It’s a sample script, now you have to set your scenario in this script and check it.

Thanks for the support @NCP it helps me alot and my requirements are been fulfilled.