diff --git a/frappe/contacts/address_and_contact.py b/frappe/contacts/address_and_contact.py index 9ccffab996..fc938244f7 100644 --- a/frappe/contacts/address_and_contact.py +++ b/frappe/contacts/address_and_contact.py @@ -146,4 +146,18 @@ def filter_dynamic_link_doctypes(doctype, txt, searchfield, start, page_len, fil order_by="dt asc", as_list=True) all_doctypes = doctypes + _doctypes - return sorted(all_doctypes, key=lambda item: item[0]) + + valid_doctypes = [] + user_roles = frappe.get_roles() + for ctype in all_doctypes: + if frappe.db.exists('Custom DocPerm', {"parent": ctype[0]}): + dtype = 'Custom DocPerm' + else: + dtype = 'DocPerm' + + valid_doctypes.append(frappe.db.get_all(dtype, { + 'parent': ctype[0], + 'role': ('in', user_roles) + }, 'distinct parent', as_list=True)) + + return sorted(valid_doctypes, key=lambda item: item[0])