Copy Child table data to another child table on different doctype failed

Hi all,

I need some assistance/guide here.
I have 2 doctypes

  1. Target Doctype : Sales Order
    1.1 Target child table :- Sales Order Item (items)

  2. Source Doctype : Item
    2.1. Source child table : Item Customer Detail (customer_items)

image



My scenario/use case is :
in target child table (sales order item), there is 1 field name customer item description which should copy/fetched from source child table (Item Customer Detail) field name ref description. I’ve tried with numbers of tutorials listed in this forum but none of them able to fetch the data.

codes :

frappe.ui.form.on('Sales Order', {
   onload: function (frm) {
    if (frm.doc.item_code) {
        frm.clear_table('items');
        frappe.model.with_doc('Item', frm.doc.item_code, function() {
            let source_doc = frappe.model.get_doc('Item', frm.doc.item_code);
           $.each(source_doc.customer_items, function (index, source_row) {
               
              var addChild = cur_frm.add_child("items");
              addChild.ref_description = source_row.ref_description;
              frm.refresh_field("items");
            });
        });
    }
  }
});
  1. Fetch data from a DocType’s child table to another’s

  2. (Client Side Scripting)Fetching child tables · frappe/frappe Wiki · GitHub

  3. Fetch values from a child table to another child table

No error no anything. Nothing is prompted for me to trace back the error. Can anyone help me to point out where/why it is not working ? And where should I make the corrections ? let me know if you need any other information. I’ll attach on the next reply.

Use below

frappe.ui.form.on("Booking", {
    "project": function(frm, cdt, cdn) {
        if (frm.doc.project) {
            frappe.model.with_doc("Real Estate Project", frm.doc.project, function() {
                var mcd = frappe.model.get_doc("Real Estate Project", frm.doc.project);
                cur_frm.clear_table("payment_schedule");
                $.each(mcd.project_payment_schedule, function(i, d) {
                    i = frm.add_child("payment_schedule");
                    i.milestone_name = d.milestone_name;
                    i.invoice_portion = d.invoice_portion;
                });
                cur_frm.refresh_field("payment_schedule");
            });
        }
    }
});
2 Likes

Hi @mohitchechani , thank you for the code u shared, it really provides a helpful guide to me. I shared mine here as well :

frappe.ui.form.on("Formulation Table", {
item_code: function(frm, cdt, cdn) {
	var row = locals[cdt][cdn];
	if (row.item_code) {
	   
		frappe.model.with_doc("Item", row.item_code, function() {
			var doc = frappe.model.get_doc("Item", row.item_code);
			
			$.each(doc.customer_items || [], function(i, r) {
			
			if(r.customer_name == frm.doc.customer) {
			
				i = frm.add_child("formulation_table");
				i.ref_code = r.ref_code;
	    	}	
			 	
			});
			
			cur_frm.refresh_field("formulation_table");  
		});			 
      }
 }
});

@bella.zahar
Hello i am trying to fetch data from child table to another but it is not working with me
i want to fetch the qty in sales order item table at sales order doctype to delivery note item table at delivery note doctype but it not working can you help me in this
this is my code
frappe.ui.form.on(“Delivery Note”, {
on_load: function(frm) {

if(frm.doc.sales_order) {

  frm.doc.items.forEach(function(dn_item) {

    var sales_order_item = dn_item.sales_order_item;

    if(sales_order_item) {

      frappe.model.with_doc("Sales Order Item", sales_order_item, function() {

        var so_item = frappe.model.get_doc("Sales Order Item", sales_order_item);
        
        var qty = so_item.qty;

        frappe.model.set_value(
          "Delivery Note Item", 
          dn_item.name,
          "qty_ordered",
          qty
        );

      });

    }

  });

  frappe.run_serially(function() {
    frm.refresh();
  });

}

}
});

@yara
If you create DN from Sales Order it should be fetched automatically or if you create DN, you can have Get Items From> Sales Order to import everything. It is not clear why you need to do it with script. This is a working script:

@rapidweb
Thank you for your reply
i want to fetch qty ,delivery date and delivered qty to make a custom print format and show remaining qty because the company sales to customer and deliver them in separate times and qty
it not fetched automatically although in other doctype when i add custom field with same label it fetched from the linked document
i will try your code thanks