How to add a child table after creating a new document using frappe.new_doc() in JavaScript?

I’m working on a custom script in Frappe where I need to create a new document and then add entries to its child table using JavaScript. I’m using frappe.new_doc() to create the parent document, but I’m unsure how to proceed with adding child table entries programmatically.

Here’s what I have so far:

Any help or examples would be greatly appreciated.

Please check the reference and video:

Thank you so much for the assistance! It’s working like a charm now.

Below is the updated code snippet I’m currently using:

@NCP Is there any way to set default values for all fields? For instance, if I add an item_code in the item table, is there a method to automatically set default values for all fields in the document, similar to triggering an event?

pr_item.item_code = "Item A";
pr_item.qty = 10;

try it.

Yeah, I’ve tried that, but it’s not working as expected.

if I set the default values programmatically, it would look like this:

if I add the values through the UI, it would look like this:

@NCP This is my current code snippet:

Use it.

frappe.model.set_value(row.doctype, row.name, "item_code", i.item_code);

@NCP Not working, I also cleared the cache and tried twice, but still getting the same result

my code:

Worked from my end.

@NCP The code works when run in the web console, but it doesn’t work when executed through the code

@NCP I tested it with a setTimeout of 1 second, and it worked. So my guess is that if I run this after the document is ready, it will work fine. Before that, it doesn’t work. Is there any way to determine if the document is ready

I have given the reference of the video here, maybe it will help someone.

@NCP Thanks for your time and this video. It will be very helpful!