To delete a row from child table

Hey guys,
I have a scenario that I have to delete a row of a child table in a doctype.docname when a field “sign_off” of another doctype.docname is not None (when entering a value and validated).
The 2 doctypes have no connections other than some common fields that are linked such as “employee_id” , “selection_checklist” etc.
I am sharing my code , but it’s not working .

def after_insert(self):
	for lists in self.table_1:

		if lists.sign_off is not None:
			get_crew = frappe.get_doc("Crew", lists.vessel)
			to_remove = []
			for rows in get_crew.crew_details:
				if lists.pp_no == rows.passport_number:
					to_remove.append(rows)
				for i in to_remove:
					if rows.passport_number == i.passport_number:
						get_crew.remove(rows.passport_number)
				get_crew.save()

Can anyone help me to solve this ?

1 Like

Try this @Sebin_P_Sabu

def after_insert(self):
	for lists in self.table_1:

		if lists.sign_off is not None:
			get_crew = frappe.get_doc("Crew", lists.vessel)
			to_have = []
			for rows in get_crew.crew_details:
				if not lists.pp_no == rows.passport_number:
					to_have.append(rows)
				get_crew.update({'crew_details':to_have})
				get_crew.save()
	frappe.db.commit()
1 Like

@Ponnusamy_V Hy, 1st of all thanks for replying.

I used your code and it was not working. So , I changed after_insert to validate and the code worked. But instead of deleting the row of that particular passport_number, it was deleting the entire table.

So I made some changes , and is now working :blush:
The code is ,

def validate(self):
	for lists in self.table_1:
		if lists.sign_off is not None:
			get_crew = frappe.get_doc("Crew", lists.vessel)
			to_have = []
			for rows in get_crew.crew_details:
				if not lists.employee_id == rows.employee_id:
					to_have.append(rows)
			if to_have:
				get_crew.update({'crew_details':to_have})
			get_crew.save()
	frappe.db.commit()

Thank you so much for your help.The credit goes to you as I just changed some lines. Thank you.

1 Like