Insert row in child table automatically

My task now is to create a button named ‘Add book’. After I click it a dialog box has to appear with link field with some already specified book names. After I click on one, I want the details of that particular book like author, genre, isbn, etc to automatically fill in the child table named ‘books_table’ in the respective fields, namely author, isbn, etc.

Hi @Suraj_Jai_krishna ,
we can get values from dialog box using primary action.
Kindly refer Dialog API
Then,
var child_table_row = frm.add_child(“child_table_field_name”)
child_table_row.author_name = values[‘author’]

1 Like

Hi @Suraj_Jai_krishna,

Please check the code and apply it.

frappe.ui.form.on('Your DocType', {
    refresh: function(frm) {
        frm.add_custom_button(__('Add Book'), function() {
            // Open a dialog box with input fields for author, genre, isbn, etc.
            frappe.prompt([
                {label: __("Author"), fieldname: "author", fieldtype: "Data", reqd: 1},
                {label: __("Genre"), fieldname: "genre", fieldtype: "Data", reqd: 1},
                {label: __("ISBN"), fieldname: "isbn", fieldtype: "Data", reqd: 1},
                // Add other fields as needed
            ], function(values){
                if (values.author && values.genre && values.isbn) {
                    // If all required values are provided, set them into the child table
                    frm.add_child('books_table', {
                        'author': values.author,
                        'genre': values.genre,
                        'isbn': values.isbn,
                        // Add other fields as needed
                    });
                    refresh_field('books_table');
                } else {
                    frappe.msgprint("Please provide all required information.");
                }
            }, __("Enter Book Details"));
        });
    }
});

Please specify the doctype, the name of the field, the type of field, the name of the field in the table, and the field set in the script as per your requirements. After that, reload and verify it.

I hope this helps.

Thank You!

4 Likes

Thank you so much @Praveenkumar !:slight_smile:

Thank you so much @NCP !! It really worked. A sincere thanks to u!!

can u even tell how to add row without requiring to refresh page? cos it appears only after i refreshed it.

I think, without refresh doesn’t work.

Hmm…kk…seems like that only…

It all worked well. Really thank u so much!!!

1 Like