Add row to custom table upon validate in Purchase Order


We have created a custom field - table (approval_workflow) in Purchase Order. And upon validate, we add_child to the table. However, the added row appears for a short second and disappears.
We are trying to understand the methods in Purchase Order. We do want the method to be at validate. However, is there another method after the Save that refreshes the code? Currently, even after the add_child the table shows no data after Save - the row disappears after appearing for a few seconds. Kindly advice how to add_child to our custom table upon Save at Purchase Order. Thank you.

Following is the add_child method at Custom Script triggered upon validate :

frappe.ui.form.on("Purchase Order", "validate", function(frm, dt, dn) {{
		method: "organizational_management.organizational_management.doctype.purchase_order_approval_workflow.purchase_order_approval_workflow.get_items",
		args: {
			form : cur_frm.doc
		callback: function(r) {
			var e = cur_frm.add_child('**approval_workflow**');
			e.action = "Apply";                     

@asneha1 please format your code. (see my edits)

You cannot do an ajax call on validate via JS

You have to write a validate hook in your custom app for Purchase Order

@rmehta, thank you for your response and kindly formatting the code, will follow your standard moving forward.

If we were to write a validate hook, would that mean, we write statements in SQL to directly add to the child table?
How do we then take care of displaying the added rows in the child table on Purchase Order?

@asneha1 your write in function

def add_rows(doc, method):
   '''called before save via hooks'''
   doc.append("items", {"item_code": "X", "qty": 1})

@rmehta, thank you!
We have tried it and it worked! Really appreciate.

Similar to doc.append(“items”, {“item_code”: “X”, “qty”: 1}),
is there a method to remove rows from the table?

Hi @asneha1,

Please refer Remove Row to remove the rows from child table.

Thanks, Makarand

1 Like

@makarand_b, thank you so much!
Tried, worked perfectly.