Hi all,
I’ve created a custom Doctype called Service Request under my custom app. I’ve implemented custom validation inside the on_cancel()
method to prevent cancellation if certain linked documents (like Stock Entry or Sales Invoice) exist.
Here’s a simplified version of my code:
def on_cancel(self):
super().on_cancel()
self.ignore_linked_doctypes = ("Serial and Batch Bundle",)
self.validate_cancel()
self.check_nextdoc_docstatus()
def validate_cancel(self):
stock_entry = frappe.db.sql(
"""select name from `tabStock Entry`
where custom_service_request = %s and docstatus = 1""",
self.name,
)
if stock_entry:
frappe.throw(
_("Cannot cancel because submitted Stock Entry {0} exists").format(
frappe.utils.get_link_to_form("Stock Entry", stock_entry[0][0])
)
)
def check_nextdoc_docstatus(self):
linked_invoices = frappe.db.sql_list(
"""select distinct t1.name
from `tabSales Invoice` t1, `tabSales Invoice Item` t2
where t1.name = t2.parent and t2.custom_service_request = %s and t1.docstatus = 0""",
self.name,
)
if linked_invoices:
linked_invoices = [get_link_to_form("Sales Invoice", si) for si in linked_invoices]
frappe.throw(
_("Sales Invoice {0} must be deleted before cancelling this Service Request").format(
", ".join(linked_invoices)
)
)
But when I try to cancel a submitted Service Request, I get the default popup:
“Cancel All Documents — Do you want to cancel all linked documents?”
Even if I click “Cancel All”, my custom logic doesn’t run. It seems like the system is stopping cancellation before on_cancel()
is triggered.
How can I resolve this issue and ensure my custom on_cancel()
logic is triggered properly?
Thanks in advance!