How to prevent Edit and/or Delete on DocType?+

I have a “status” doc_field… I need to block edit and/or delete depends of the “status” value…

Where is the better approach and how can I do that?

Thanks!

1 Like

Change role permission for the particular user.

Hello @rk_root I do not want to prevent edit/delete from user… lI want like these: if status = ‘A’ doesn’t edit/delete… just need to check the status, depend of the value of the status, user can edit/delete .

Have you tried hook? by creating a method and hook it with on_trash and on_update or on_change, check if status cannot be edited or deleted, throw an error.
https://frappe.io/docs/user/en/guides/basics/hooks

I read the link, but, I do not understand how to apply these to my issue…

Could you, please, give me a example how to do that?

thanks!

Here’s a question like yours with a response that may help? Hooks events script

Here’s hook example when delete Contact

Method example, update_issue (erpnext.support.doctype.issue.issue.update_issue)

When event hook called, it passes two parameters. Doctype object , method. Like example above, it passed contact doctype object and event (method) as string “on_trash”.

Regarding your issue:
let’s say your doctype is Contact and you want to prevent user to edit or delete when status == “ready”

Assuming you have custom app, your hook would be:

"Contact":{
		"on_update": "customapp.modulename.doctype.contact.contact.check_before_edit_delete",
        "on_change": "customapp.modulename.doctype.contact.contact.check_before_edit_delete",
        "on_trash": "customapp.modulename.doctype.contact.contact.check_before_edit_delete",
	}

Your method would be: (customapp.modulename.doctype.contact.contact.py)

class Contact(Document):
    pass

def check_before_edit_delete(doc, method):
    if(doc.status == "ready"):
        frappe.throw(_("This record is not allow to edit or delete"))
1 Like