Adding new row to a child table using PUT call

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?

Hi @Rabie_Moses_Santilla,

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!

Thank you for your reply sir. How can I use .append with this sir.

Hmm :thinking:
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.

3 Likes

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.)

4 Likes

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.