Try ERPNext Try Frappe Cloud Buy Support Partners Foundation

How to automatically create rows in Child Table based on another Doctype

Hi, I have three doctypes, I’ll name them and their respective fields to ease use.

  1. Doctype A
    1 Child Table
    2 Has two fields : field A and field B ( i.e doc.a & doc.b)
  2. Doctype B
    1 Parent Doctype of Doctype A
    2 Two fields : field 1 (a link type field, linking to User), field 2 (table field linked to
    Child Table Doctype A)
  3. Doctype C
    1 A simple doctype with three fields: field 1 (A link to User), field 2 (Data Field), field 3 (Data field)

Now what I want to do is, automatically fetch and add rows to Doctype A table in Doctype B whenever I create a Doctype C.

Example:
I create a Doctype C and enter the following Data into the Document:

  1. Field 1 : user@abc.com (The link to the User)
  2. Field 2: “Some Data”
  3. Field 3: “Some More Data”

Now I create Multiple Documents for Doctype C with different Data entered for Different users. Let’s say there are multiple Documents in which the selected user is user@abc.com in Field 1.

What I want to do is whenever I create a Doctype B and Select the User user@abc.com in the first field, I want all Doctype C with the user@abc.com in the first field automatically added to the table below.

Example:
Doctype B:
User: user@abc.com
Table: (All Doctype C documents in which user is user@abc.com).

I want the data to be added automatically rather than me linking it to the Doctype itself manually and fetching the Data. I want an automatic prompt to fetch all Documents into the Child table for Doctype C, filtered on the basis of the entered user.

If anyone could cite similar code which I can mould to work for me or link me to a thread where I can find said code Id be very grateful as I’ve searched the threads since long and haven’t found a solution for a problem similar to mine.

It’s not entirely clear to me what you’re trying to do. Generally speaking, you can do whatever you want to Doctype B by catching the on_insert or on_save events in the (python) controller object. You’d just make a frappe.get_all() call filtered by username, then add those as children to doctype B.

More broadly, it sounds like you’re trying to use doctypes to do the job of reports. Does doctype B record new information? Or does it just summarize data from doctype C?

Yes I am trying to somewhat replicate the job of reports but within a doctype. Let’s say a user has a document called “User Files Checklist”. In it is some data about the user and a Table which contains all the “Files/Documents”. The data (“Files”) in the table should be fetched automatically rather than me adding rows to the table myself. I can add a link field to the table and add the “Files” myself but I want this whole process automated, filtered on the basis of the user’s name.

Doctype B only summarizes data from Doctype C, it is basically a shell doctype, made to convert and show data from Doctype C into tabular form.

That’s generally considered a bad design pattern. If you really want to do it, though, it’s just a matter of using the hooks and database pulls I mentioned in the last post.

Surely, but I am rather new to programming. I can read, understand and modify code to suit my use but I need something to work on. If you know of a thread which cites code to a similar situation as mine, please mention that. I’d really appreciate that

I can’t think of anything that uses logic similar to what you’re trying to do. Summary doctypes like this are generally a bad idea, since they violate the principle of single-source truth.

If you’re looking for examples of adding to child tables programmatically, a quick search on this forum turns up lots of examples. For example: