Frappe Cloud Support Partners Foundation Frappe School

Frappe.db.insert() with child table

I have a custom doctype. From this doctype I want to create a new document to Contact doctype using a custom button.

I successfully use this js in the frm.add_custom_button function for the button:

   "doctype": "Contact",
   "first_name": frm.doc.first_name,
   "last_name": frm.doc,last_name

But in Contact there is also a child table for phone numbers.
In my custom doctype I have the fields for phone number.
How do I insert this phone number to the child table?

I also tried to use below script but I don’t think it will work as it is new so there is no docname yet:

var p = frappe.model.add_child(docname, "Contact", "phone_numbers");
frappe.model.set_value(p.doctype,, "phone", frm.doc.nomor_hp);

Thank you

Hope this helps to sort the issue.
contact = frappe.new_doc(“Contact”)
contact.field = your field value
contact.append(“phone_nos”, {
‘phone’: ‘xxxxxxxx’

Isn’t that for python?
I need the js one.

The alternative is to use and python method (I already have the code for the python method for a code triggered insertion):

def add_contact(self):
    contact = frappe.new_doc('Contact')
    #the rest of the code to insert values to Contact

But another question arise: how to pass values to a python method asking for (self)?

That is for python. Instead of trying in js, py is better for creating a new_doc with child table details.
Pass the values from js as frm.doc and convert the json in py.

The py method I already have takes self as argument (because it takes data from a form), so in the py:

def add_contact(self):
    contact = frappe.new_doc('Contact')
    contact.first_name = self.firstname
    contact.last_name = self.lastname
    contact.append('phone_nos', {
        'phone': self.nomor_hp,
        'is_primary_mobile_no': 1,

How do I convert the frm.doc to self (how the args in the should be)?

Try this by Converting the json arg
args - represent the frm.doc
a = json.loads(args)