I want to insert a doc for my custom DocType on submission and cancel of a “Sales Order” and couldn’t find a function like “frappe.insert_if_not_exists” or similar.
So I catch the exception “frappe.DoesNotExistError” thrown on frappe.get_doc().
And everytime a message pops up with “[docname] not found” but then creates it, because I’m catching the error.
But I don’t want the message box to pop up.
How could I prevent this?
Or am I doing things wrong entirely and there is a function that I couldn’t find that does just that?
Here is my code:
def update_reserved_qty(self, method):
for item in self.items:
if item.batch_no:
try:
br = frappe.get_doc('Batch Reservation', "%s - %s" % (item.batch_no, self.name))
except frappe.DoesNotExistError:
frappe.msgprint("Creating Doc cuz not exist")
br = frappe.get_doc({
'doctype': 'Batch Reservation',
'ref_doctype': self.doctype,
'ref_docid': self.name,
'batch_no': item.batch_no,
})
br.insert()
reserved_qty = br.db_get('reserved_qty')
if method == 'on_submit':
reserved_qty += item.qty
elif method == 'on_cancel':
reserved_qty -= item.qty
br.db_set('reserved_qty', reserved_qty)