Child Table Link Field Population

Hi,
I have a doctype, let’s call it Invoice List.
Now Invoice List has two fields:

  1. participant : A link field to a doctype called Participant
  2. Invoices : A Child Table

There is another Doctype called Invoice.
Invoice has two fields:

  1. participant : A link field to Participant
  2. Total : A currency field

The child table Invoices has two fields:

  1. Invoice : A link field to Invoice doctype
  2. Total : The total field fetched from Invoice

Now, I intend for the child table to populate the link field Invoice based on the participant. I want all Invoice documents with the participant USER ABC to be auto-added to the child table where the participant is USER ABC in the Invoice List.

Currently the way I’m approaching this is to push changes to two extra fields in the Invoice List whenever I create an Invoice, then to add it to the child table and finally clear the two extra fields but this whole logic is flawed fundamentally. Any help is appreciated.

You can search for the invoices of particular participant from the Invoice table using frappe ORM

frappe.db.get_list(“Invoice”,filters={‘participant’:‘USER ABC’},fields={‘participant’,‘total’})

and then can populate your child table in js of the Invoice list doctype using below sample code

  frappe.model.clear_table(frm.doc, "Child Table Name")
	frappe.call({
		method: 'Your method in py file for fetching records',
		args: {
			'participant':frm.doc.participant
		},
		callback: function (r) {
			if (r.message) {
				var row;
				for (row in r.message){
					var childTable = frm.add_child("Child Table Name");
					childTable.'DOC-FIELDNAME' =r.message[row]['FieldName'];
				}
				frm.refresh_fields("Child Table Name");
			}
		}
	});

This snippet I’m supposed to add to a defined method in the .py file?

Yes, this should be in py file

That didn’t work. I’ll share my full details here:

Doctype A : Invoice PM
Has field total.

Doctype B: Participant Funding
Has child table : Invoices

Child Table Invoices
Has two fields:

  1. Invoice (link to Invoice PM)
  2. Total (Fetched value from linked field)

.py file for Participant Funding:

class ParticipantFunding(Document):
	def servicedefiner():
		frappe.db.get_list("Invoice PM",filters={'participant':self.participant},fields={'participant','total'})

.Js file for participant funding:

frappe.ui.form.on("Participant Funding", {
	refresh(frm) {
        frappe.model.clear_table(frm.doc, "Invoices");
		frappe.call({
		method: 'servicedefiner',
		args: {
			'participant':frm.doc.participant
		},
		callback: function (r) {
			if (r.message) {
				var row;
				for (row in r.message){
					var childTable = frm.add_child("Invoices");
					childTable.invoice =r.message[row]['invoice'];
				}
				frm.refresh_fields("Invoices");
			}
		}
	});
	}
});

also changed the method from:
method: 'servicedefiner'
to
method:'app.module.doctype.participant_funding.participant_funding.servicedefiner'

You aren’t passing argument to servicedefiner. It should be like that

def servicedefiner(self, participant=None):
	frappe.db.get_list("Invoice PM",filters={'participant':participant},fields={'participant','total'})

Changed the code. Still doesn’t work :pensive:

As a pro point you should defined your function as static available outside the scope of class like this

@frappe.whitelist()
def servicedefiner(self, participant=None):

Can you share screenshot

sorry, remove self


Why aren’t you returning result from function

Um I dont know how to. Should I type:

return invoice ?

Also in client side script you are triggering code on refresh event. It is obvious no participant is selected.
Change refresh to participant
Also add retrun in py file before frappe.db.get_list

Should I just type return before frappe.db.get_list?

         @frappe.whitelist()
	def servicedefiner(participant=None):
		return frappe.db.get_list("Invoice PM",filters={'participant':participant},fields={'participant','total'})

Done that, changed refresh(frm) to participant: function(frm). Still doesn’t work. It makes that error sound whenever I change the participant field and the child table doesnt update :pensive:

Share screenshot again


For the method:
g1 is the app name, plan management is the module name