Fetch data from a DocType's child table to another's

Hi, I’m new around here

I’m trying to fetch data from a child table to another.

I have a DocType called “Cirugia”
(image in link)

and another DocType called “Solicitud de Remision”
(image in link)

each one of them have their own child table for products

DT Cirugia’s child table called: Almacen_cirugia
(image in link)

DT Solicitud de remision’s child table called: Almacen Solicitud de remision
(image in link)

Link for images: https://imgur.com/a/MBVBUYP (I couldnt upload images, dont know why )
I need to complete de form “Solicitud de Remision” and when I give Cirugia’s ID, it can fetch data from Cirugia DocType, but I need it to take data from the Cirugia’s child table to the Solicitud de remision’s child table (automaticlly)

How could I do this? I’ve tried these codes but they are not working for me

frappe.ui.form.on("Solicitud de Remision", {
	refresh: function(frm) {
		frm.add_fetch("id_de_la_cirugia", "fecha_cir", "fecha_de_cirugia");
		frm.add_fetch("id_de_la_cirugia", "unidad_médica_cir", "unidad_medica");
                //the code above works well for fetching data between DocTypes
                //this line is one of the solutions that I tried but doesn't work
                frm.add_fetch("id_de_la_cirugia", "consumos", "in_sol_rem");
	}
});

//and  tried this code that I took from github but didnt work for me
/*
frappe.ui.form.on("Solicitud de Remision", {
    "id_de_la_cirugía": function(frm) {
        frappe.model.with_doc("Cirugia", frm.doc.id_de_la_cirugia, function() {
            var tabletransfer= frappe.model.get_doc("Cirugia", frm.doc.id_de_la_cirugia)
            $.each(tabletransfer.Almacen_cirugia, function(index, row){
                var d = frm.add_child("Almacen Solicitud de remision");
                d.cod_pro_rem = row.codigo_de_producto;
                d.cant_sol_rem = row.cant_pro_alm;
                d.des_sol_rem = row.descripcion;
                d.uni_sol_rem = row.precio;
                d.tot_sol_rem = row.monto_total_de_la_remision;
                frm.refresh_field("Almacen Solicitud de remision");
            });
        });
    }
});*/

Any Ideas? !help

1 Like

I also tried

frappe.ui.form.on("Solicitud de Remision", "onload", function(frm) {
    frappe.model.with_doc("Cirugia", frm.doc.trigger, function() {
        var tabletransfer= frappe.model.get_doc("DocTypeA", frm.doc.Trigger)
        $.each(qmtable.Almacen_cirugia, function(index, row){
        d = frm.add_child("Almacen Solicitud de remision");
        d.cant_pro_alm = row.cant_sol_rem;
        d.codigo_de_producto = row.cod_pro_rem;
        cur_frm.refresh_field("Solicitud de Remision");
        })
    });
});

Try using frappe.call method for fetching data into one child table to another…!!

refer to this

where can I use it? and how? can you give me an example?

```
frappe.ui.form.on("[CHILD_TABLE_TARGET_DOCTYPE]", {
	[CHILD_TABLE_LINK_FIELD]: function(frm, cdt, cdn) {
		var row = locals[cdt][cdn];
		if (row.[CHILD_TABLE_LINK_FIELD]) {
			frappe.model.with_doc("[SOURCE_DOCTYPE]", row.[CHILD_TABLE_LINK_FIELD], function() {
				var doc = frappe.model.get_doc("[SOURCE_DOCTYPE]", row.[CHILD_TABLE_LINK FIELD]);
				$.each(doc.[SOURCE_DOCTYPE_CHILD_TABLE] || [], function(i, r) {
					if(r.[SOURCE_CHILD_TABLE_FIELD] == frm.doc.[TARGET_PARENT_DOCTYPE_FIELD]) {
						var df = frappe.meta.get_docfield("CHILD_TABLE_TARGET_DOCTYPE","[TARGET_CHILD_TABLE_CUSTOM_FIELD]", frm.doc.name);
						df.options += ["\n" + r.[TARGET_CHILD_TABLE_CUSTOM_FIELD]];
					}
				})
			});
			frm.refresh_field("[TARGET_DOCTYPE_CHILD_TABLE]")
		}
	}
});
```

This code works just fine, if you can’t get it to work check your syntax for errors

1 Like

I was reading it, but I had a problem, my intention is to select something in a field of my doctype A, once it’s selected, it has to do fetching between child tables (C) and (D), in that code I have to put a custom field inside of my child table (C) but that’s not my intention, can you help me out with this problem? :slight_smile:
thx for your response

Just set your trigger as the field you’re changing. Share screenshots of your exact doctypes, your child doctypes and the related fields

Sure, this is my Doctype (A), the field in blue is my trigger and the red is my child table

and this is the another doctype (B) and it’s child table (D)

This is my child table (C) I need to fetch all these fields

From this another child table (D)

frappe.ui.form.on('Solicitud de Remision', {
    id_de_la_cirugia: function (frm) {
        if (frm.doc.id_de_la_cirugia) {
            frm.clear_table('in_sol_rem');
            frappe.model.with_doc('Cirugia', frm.doc.id_de_la_cirugia, function () {
                let source_doc = frappe.model.get_doc('Cirugia', frm.doc.id_de_la_cirugia);
                $.each(source_doc.consumos, function (index, source_row) {
                    
                var addChild = frm.add_child("in_sol_rem");
	             	addChild.importe_unitario = source_row.importe_unitario;
		            addChild.des_sol_rem = source_row.descripcion;
		            // add as many fields as you want
		            frm.refresh_field('in_sol_rem');
                });
            });
        }
    }
});

Make sure the field types match for both child tables to map them

5 Likes

It’s working perfectly, I just changed some variables like :slight_smile:

addChild.uni_sol_rem = source_row.importe_unitario;
		            addChild.des_sol_rem = source_row.descripcion;
		            addChild.tot_sol_rem = source_row.subtotal;

thank you so much! god bless you! greetings from Mexico :slight_smile:

Hello @Vesper_Solutions I have a requirement like I have a button in DOCtype1(with Child table) and when i click this button I want the values of child table to be created in DOCType2 child table.

Is this possible using Custom Script?

Thanks in advance!!

Look at this post, the solution I’ve posted at the end is used for populating a child table. You need to slightly modify the code to populate the child table for Doctype2.

Fantastic!

Hello,
Thank you for your Solution. I have similar problem and tried this code but unable to fetch the tables after selecting the trigger field.
Below is my ‘Source Doc &Table’ , ‘Target Doc & table’ and ‘Script’
It will be very helpful if guys verify & rectify me
Thanks a lot.





If you want to fetch data in child tables then you can do it by add_fetch.

Note: Source field (Link Field) also should be in child table.

Example:

Parent name `Job` (DocType name) have one  child table name `Reference` (Doctype Name).
Child table have fields `Task` (Link Field) and one field we have to fetch data `Left Time` .
When `Task` selected we need `Left Time`.

In Job’s js file

frappe.ui.form.on("Job", {
     setup: function (frm) {
		frm.add_fetch(
			"task",
			"left_hours",
			"left_time",
			"Reference"
		);
      },
});
  • task : Field name from we need data (Link Field).
  • left_hours : Field which have to be fetch.
  • left_time: Field to set data.
  • Reference: Child table Doctype Name.