How I can insert multi rows in Child Table using Client Script (frappe.db.insert())?

Hi Guys,
How I can insert multi row of child table using db.insert

in my case I have Custom (Parent and child) Doctype and when Click on button that create sales invoice and child table (Custom) is should be insert in Sales invoice items

but I can’t insert multi rows in this way, here I use [0] for just specific row

frappe.ui.form.on('Reservation', {
    refresh: function(frm, cdt, cdn) {
        if(frm.doc.res_status == "Check In Paid"){
            frm.add_custom_button(__("Pay & Renew"), function () {
                frappe.db.insert({
                    'doctype': 'Sales Invoice',
                    'guest_name': frm.doc.guest,
                    'customer': 'Guest',
                    'cost_center': 'Baghdad - BH',
                    'selling_price_list': 'Per Room',
                    'docstatus':1,
                    'is_pos':1,
                    'payments':[
                        {
                            'mode_of_payment': 'نقد',
                            'amount':frm.doc.reservation_pricing,
                            'account':'1110 - نقد - BH'
                        }
                    ],
                    'sales_partner': frm.doc.company_name,

                   // here I want pass more than one row, now I use for specific row
                    'items': [
                        {
                            'item_code' : frm.doc.rooms[0].room_number,
                            'item_name' : frm.doc.rooms[0].room_number,
                            'qty':frm.doc.number_of_night
                        }
                    ]

                }).then(doc => {
                    console.log(doc.name);
                    console.log(cstr(frm.doc.rooms.length));
                    var row = frappe.model.add_child(frm.doc, "Sales Invoice Reservation", "sales_invoice");
                    row.sales_invoice = doc.name;
                    frm.refresh_fields("sales_invoice");
                    frm.save();
                });
            }).addClass("btn-danger");    
        }
    }
});

@Omar_Mohammed why not using 'items ’ : frm.doc.rooms . it doesn’t matter it’s a table

1 Like

@bahaou
I try it, but I don’t have all (Sales invoice items) filed, I just want to send (room_number) as (item_code) and (number_of_night) as (item_qty)

when I use ‘items’:frm.doc.rooms
this is the result
Screenshot 2022-06-30 185927

@Omar_Mohammed Could you just map the room array to the items key like this ?

frappe.ui.form.on('Reservation', {
    refresh: function(frm, cdt, cdn) {
        if(frm.doc.res_status == "Check In Paid"){
            frm.add_custom_button(__("Pay & Renew"), function () {
                frappe.db.insert({
                    'doctype': 'Sales Invoice',
                    'guest_name': frm.doc.guest,
                    'customer': 'Guest',
                    'cost_center': 'Baghdad - BH',
                    'selling_price_list': 'Per Room',
                    'docstatus':1,
                    'is_pos':1,
                    'payments':[
                        {
                            'mode_of_payment': 'نقد',
                            'amount':frm.doc.reservation_pricing,
                            'account':'1110 - نقد - BH'
                        }
                    ],
                    'sales_partner': frm.doc.company_name,

                   // here I want pass more than one row, now I use for specific row
                    'items': frm.doc.rooms.map(room => {
                            'item_code' : room.room_number,
                            'item_name' : room.room_number,
                            'qty':frm.doc.number_of_night
                        })
                   

                }).then(doc => {
                    console.log(doc.name);
                    console.log(cstr(frm.doc.rooms.length));
                    var row = frappe.model.add_child(frm.doc, "Sales Invoice Reservation", "sales_invoice");
                    row.sales_invoice = doc.name;
                    frm.refresh_fields("sales_invoice");
                    frm.save();
                });
            }).addClass("btn-danger");   
1 Like

@dj12djdjs
Thanks for your reply, but can you give me documentation for using map in frappe
because I have syntax error
2022-07-01 00_56_38-Window

@Omar_Mohammed map is a JavaScript utility Array.prototype.map() - JavaScript | MDN

Also, don’t use ; inside of the object. This is invalid JavaScript

I search for arr.map and I found any function should return some thing and I edit it code and is Working good :100: :100:

                    'items': frm.doc.rooms.map((room) => {
                           return {
                                'item_code' : room.room_number,
                                'item_name' : room.room_number,
                                'qty':frm.doc.number_of_night
                            };
                        })

Thanks @dj12djdjs

1 Like