Child Table Link Field Population

Then why you are using cdt and cdn.

I’ll remove and try

Same result

Not populating

Yeap, its not populating the child table

put refresh fields after childtable.invoice and see what happens also do console.log of ```
response.message[row][‘docname’

And share screenshot

Console:

Code:

why you are using cur_frm use frm

Done, same result

can you come online

Thanks a lot mate but how about tomorrow? I’m sure it’s late for you too?

No worries. Literally I am confused why this code is not working for you. I have used it numerous times and even I think this is standard

I’ve got the code to slightly work. It’s now adding to the child table but the response message it adds is this: [object Object].

Here’s the code:

frappe.ui.form.on("Participant Funding",{
    verify: function(frm){
        frappe.call({
        method:"frappe.client.get_list",
        args:{
            doctype:"Invoice PM",
            filters: [
                ["participant","=", frm.doc.participant]
            ],
        fields:["docname"]
    },
    callback: function (response) {
        if (response.message) {
            var row;
            $.each(response.message, function(i) {
                var r1 = cur_frm.add_child("invoices");
		        r1.invoice = response.message; // error is here
                        console.log(response.message);
					});
            frm.refresh_fields("invoices");
        }
    }
});
}
});

This is what it adds:

Managed to fix it. I’ll add my code to serve a general use case for the community.

Doctype A:

  1. field_a (could be any field type)
  2. child_table_DoctypeA (a child table)

child_table_DoctypeA:

  1. child_table_field_a (could be any field type)
  2. child_table_field_b (could be any field type)

Doctype B:

  1. field_a (could be any field type)
  2. field_b (could be any field type)
  3. field_c (could be any field type)

Use Case:
Let’s say you want to populate child_table_DoctypeA with specific conditions from the list of Doctype B documents based on a trigger. This method relies purely on client side scripting.
Note:
The field types from Doctype B and child_table_DoctypeA must match to be populated. As an example a signature field mapped onto a date field will yield weird results.

Here’s the code:

frappe.ui.form.on("Doctype A",{
    refresh: function(frm){   // I have used refresh you can use any trigger
        frm.clear_table('child_table_DoctypeA');
        frappe.call({
        method:"frappe.client.get_list",
        args:{
            doctype:"Doctype B",
            filters: [
                ["field_a","=", frm.doc.field_a]  // You can set any filter you want
            ],
        		fields:["field_b","field_c"] // you can fetch as many fields as you want separated by a comma
    		},
    		callback: function (response) {
        	if (response.message) {
            	$.each(response.message, function(i,row) {   // row can be anything, it is merely a name
            		    var child_add = cur_frm.add_child("child_table_DoctypeA");  // child_add can be anything
		        		child_add.child_table_field_a = row.field_b;
		        		child_add.child_table_field_b = row.field_c; // you can add as many fields as you want
              	        console.log(response.message);     // not really necessary just so you can view the message in the console to check for possible errors               
							});
          frm.refresh_fields("child_table_DoctypeA");
        	}
    		}
			});
		}
});

I’d also like to request the admins to post this on the Community Developed Scripts github page, it might help new developers? I’d add it as a contribution post but I dont know how to :slight_smile:

10 Likes

This is exactly same which I am doing with server side

1 Like

Hello @Vesper_Solutions and @tallal_habib

Sorry for such a late post on this topic but the

frm.refresh_fields("Invoices");

should be

frm.refresh_field("Invoices");

1 Like

This code [1] will work just fine, provided you invoke


frm.refresh_field("Invoices");

1 : Child Table Link Field Population - #54 by Vesper_Solutions

Hi I have a situation where i need to add a row child table in Doctype B using values from child table of Doctype A. Can Someone help me here?

Sure, cite your use case.

I want to populate field within a Doctype.

Please Help me.