How to check whether a document is linked to another document (draft)

Hi,
I actually want to stop cancellation of Supplier Quotation, if it is attached to a Purchase Order (Draft) and the Purchase Order is not submitted yet.

I have a code which I can use:

def check_if_doc_is_linked(doc, method="Cancel"):
	"""
		Raises excption if the given doc(dt, dn) is linked in another record.
	"""
	from frappe.model.rename_doc import get_link_fields
	link_fields = get_link_fields(doc.doctype)
	link_fields = [[lf['parent'], lf['fieldname'], lf['issingle']] for lf in link_fields]

	for link_dt, link_field, issingle in link_fields:
		if not issingle:
			for item in frappe.db.get_values(link_dt, {link_field:doc.name},
				["name", "parent", "parenttype", "docstatus"], as_dict=True):
				if item and ((item.parent or item.name) != doc.name) \
						and ((method=="Delete" and item.docstatus<2) or (method=="Cancel" and item.docstatus==0)):
					# raise exception only if
					# linked to an non-cancelled doc when deleting
					# or linked to a submitted doc when cancelling
					frappe.throw(_('Cannot delete or cancel because {0} <a href="#Form/{0}/{1}">{1}</a> is linked with {2} <a href="#Form/{2}/{3}">{3}</a>')
						.format(doc.doctype, doc.name, item.parenttype if item.parent else link_dt,
						item.parent or item.name), frappe.LinkExistsError)

I appreciate if anyone can let me know how to do this?

Regards
Ruchin Sharma

Hi, you can call this method in the on_cancel method of Supplier Quotation