I want to make a custom method in ERP as frappe.whitelisted func.
In this func, i want to create a new User and Guardian (Education app) as 1 single process. It means that if there is a failing condition when creating Guardian, I don’t want the User document created. (Commit and rollback method)
But the thing is when I call doc.insert() it will straight away commit the data into my erp. How to handle this case?
Is there a way to insert/create new document using Database API instead of Document API?
why don’t you try the doc.save()
i did. but still same.
the document is created everytime i call doc.save()
Here my code, hopefully you can give insight.
new_record = frappe.new_doc('User')
new_record.email = paramData["emailGuardian"]
new_record.new_password = paramData["passwordGuardian"]
new_record.first_name = selectedItem_studentApplicant.custom_guardian_full_name
new_record.send_welcome_email = False
new_record.save()
new_recordGuardian = frappe.new_doc('Guardian')
new_recordGuardian.guardian_name = selectedItem_studentApplicant.custom_guardian_full_name
new_recordGuardian.email_address = paramData["emailGuardian"]
new_recordGuardian.user = new_record
new_recordGuardian.save()
When it comes to “new_record.save()” it will straight away insert user document to the database.
If i got error in the next lines, it will become issue.
You could use a try, except condition where on any exceptions you just call frappe.db.rollback()
or else instead of saving each doc individually, just call frappe.db.commit() after the entire process is done.
thanks @Void_Moon , I understand the behaviour now…
so, when do.save() is called, the document is created/updated in the database. but since I’m not calling db.commit, all these changes can be rollback.