Add row into child table in another DocType with custom script

Hi everyone,

I’m a newbie on the custom script.
I have 3 Doctype: Teacher, Class, HistoryTeaching.

  • In Class doctype, I have a field “teacher_name” linked to Teacher.
  • In HistoryTeaching, I have a field name “class” linked to Class.
  • In Teacher, I have a field name “history” with Table type and linked to HistoryTeaching.
    When creating a new class, this class will be assigned to a teacher; I want this class appears in table “history” of that teacher.

I wrote a script below, but it not work. please tell me what wrong with this.
frappe.ui.form.on('Class', { 'after_save': function(frm) { frappe.call({ 'method': 'frappe.client.set_value', 'args': { 'doctype': 'Teacher', 'name': frm.doc.history, 'fieldname': { 'class_name': frm.doc.class_name } } }); } });

You may try calling a server side function from your custom app, through hook on save/submit event:
The py function may look like below

#On Submit Event of Doctype A Fetch field_a  to new row in table_b of Doctype B
def scenario_1(self, method):	
	target_doc = frappe.get_doc("CS Doctype B", self.linked_field_ab)
	existing_row_id = frappe.db.get_value("CS Table BY", filters={"parent": self.linked_field_ab, "field_by1": self.field_a}, fieldname="name")

	if not existing_row_id:
		target_doc.append("table_b", {
			"field_by1": self.field_a
		})
		target_doc.save()
		frappe.db.commit()
3 Likes

Thanks for your help @Mukesh_Variyani , I will try and inform the result :smiley:

@Mukesh_Variyani
Could you explain for me: what is “CS Doctype B” and “CS Doctype BY”? name of DocType B and table name, is it?
Thank you,

“CS Doctype B” name of doctype for table_b (Table in Doctype B)
“CS Doctype A” name of doctype for table_a (Table in Doctype A)

1 Like

Ok understood.
:heart_eyes:

hello, any update here? are you succeed implement the script? If yes, could you please share the script regarding your use case above?

Could you explain what is “CS Table BY”, “self.linked_field_ab”, and “fieldname=“name”” ??
thanks in advance