Data Migration: Troubleshooting Item Loading in Custom Button Script

Hi Community,

In the delivery note doctype, I’ve created a custom button labeled “Tax Invoice” that references a new doctype called “Tax Invoice.” When this button is clicked, I want to transfer data from the delivery note, such as delivery note ID, customer company address, items, and taxes, to the tax invoice doctype. However, my current client script successfully loads everything except for the items into the tax invoice doctype. What steps should I take to resolve this issue?

  frappe.ui.form.on('Delivery Note', {
onload: function(frm) {
    // Add Tax Invoice option to the Create dropdown
    frm.add_custom_button(__('Tax Invoice'), function() {
        // Get customer details from Delivery Note
        var customer = frm.doc.customer;
        var customer_address = frm.doc.customer_address;
        var contact_person = frm.doc.contact_person;
        // Get items from Delivery Note
        var items = [];
        frm.doc.items.forEach(function(item) {
            console.log("itemssss", item);
            var item_row = {
                item_code: item.item_code,
                item_name: item.item_name,
                base_rate: item.base_rate,
                qty: item.qty,
                base_amount: item.base_amount,
        console.log('Items:', items);

        // Get taxes from Delivery Note
        var taxes = [];
        frm.doc.taxes.forEach(function(tax) {
            var tax_row = {
                charge_type: tax.charge_type,
                account_head: tax.account_head,
                rate: tax.rate,
                tax_amount: tax.tax_amount,
                // Add more tax details if needed
        console.log('Taxes:', taxes);

        // Create Tax Invoice with prefilled details
        frappe.new_doc('Tax Invoice', {
            customer: customer,
            customer_address: customer_address,
            contact_person: contact_person,
            items: items, // Create a new array for items
            taxes_and_charges: frm.doc.taxes_and_charges,
            taxes: taxes,
            delivery_note_date: frm.doc.posting_date, // Include Delivery Note date
            e_way_bill_number: frm.doc.e_way_bill_number,
            in_words: frm.doc.grand_total_in_words, // Include amount in words
            delivery_note:, // Include Delivery Note ID
            buyers_order_number: frm.doc.sales_order,
            // You can add more fields if needed
        }).then(function(new_doc) {
            // Open the newly created Tax Invoice
            frappe.set_route("Form", "Tax Invoice",;