Hi, I want to add a row in my child table using a PUT call of REST API but the problem is it is replacing the contents of the child table with the data of the PUT call. How can we add a new data in the child table without erasing the existing data in the child table?
I havenât used but you try and check it.
Please use append.
Example:
DocType: Contact
Add email_id in contact email_ids table
test.append('email_ids', {
'email_id': "test@gmail.com"
})
Thank You!
Hmm
Ok.
I was thinking that you set data via the put/push method.
you can define in the body when you enter new data.
I havenât used it for integration via rest API but I used it in the server script. when doc saves then automatically data inset in contact.
Example:
contact = frappe.get_doc(dict(
doctype = 'Contact',
first_name = "First",
last_name = "Name"
))
mob_mail=contact.save()
mob_mail.append('email_ids', {
'email_id': "test@gmail.com"
})
mob_mail.append('phone_nos', {
'phone': "9876543210",
})
mob_mail.save()
Hi sir. I figured it out. using POST call and direct it to the child doctype I have added a new row.
Fun Fact:
By making an API call to the Child DocTypes directly, then the childâs controller methods (if they exist) are called automatically:
- before_validate()
- validate()
- before_save()
- on_update()
- etc.
This never happens normally. For example, if you add a controller method âbefore_save()â to 'Purchase Order Item'
or 'Sales Invoice Item'
, they are never referenced when you work from the Frappe/ERPNext browser interface.
But they are called when you make a POST API, like you demonstrated above.
This can be both helpful, but also frustrating. But I thought Iâd mention this behavior.
(Iâve spent the past 12+ months battling with Child DocTypes, and trying to make them more flexible and functional, without calling them from inside Parent DocTypes.)
Brian Pond, so writing your own server scripts would be the only way to test for âunique-nessâ and eliminate duplication during the POST of new and/or updated child data?
If youâre asking about performing a POST directly to the Child DocType? Yes, server scripts would be one solution. You could also add a SQL unique index or constraint to prevent duplication.
Iâd advise against a POST to the Child DocType, though. It does work. But is more trouble than itâs worth, in my opinion.
Thanks for the response⌠and yes, I did just end up using a SQL âunique keyâ constraint. Strange that development is not fixing the API for child tables.