def check_if_doc_is_linked(doc, method=u'Delete'):
 from frappe.model.rename_doc import get_link_fields
 link_fields = get_link_fields(doc.doctype)
 link_fields = [[lf[u'parent'], lf[u'fieldname'], lf[u'issingle']] for lf in link_fields]
 for (link_dt, link_field, issingle) in link_fields:
  if (not issingle):
   for item in frappe.db.get_values(link_dt, {link_field: doc.name}, [u'name', u'parent', u'parenttype', u'docstatus'], as_dict=True):
    if (item and ((item.parent or item.name) != doc.name) and (((method == u'Delete') and (item.docstatus < 2)) or ((method == u'Cancel') and (item.docstatus == 1)))):
     frappe.throw(_(u'Cannot delete or cancel because {0} <a href="#Form/{0}/{1}">{1}</a> is linked with {2} <a href="#Form/{2}/{3}">{3}</a>').format(doc.doctype, doc.name, (item.parenttype if item.parent else link_dt), (item.parent or item.name)), frappe.LinkExistsError)

def check_if_doc_is_linked ( doc , method = u'Delete' ) : from frappe . model . rename_doc import get_link_fields link_fields = get_link_fields ( doc . doctype ) link_fields = [ [ lf [ u'parent' ] , lf [ u'fieldname' ] , lf [ u'issingle' ] ] for lf in link_fields ] for ( link_dt , link_field , issingle ) in link_fields : if ( not issingle ) : for item in frappe . db . get_values ( link_dt , { link_field : doc . name } , [ u'name' , u'parent' , u'parenttype' , u'docstatus' ] , as_dict = True ) : if ( item and ( ( item . parent or item . name ) != doc . name ) and ( ( ( method == u'Delete' ) and ( item . docstatus < 2 ) ) or ( ( method == u'Cancel' ) and ( item . docstatus == 1 ) ) ) ) : frappe . throw ( _ ( u'Cannot delete or cancel because {0} <a href="#Form/{0}/{1}">{1}</a> is linked with {2} <a href="#Form/{2}/{3}">{3}</a>' ) . format ( doc . doctype , doc . name , ( item . parenttype if item . parent else link_dt ) , ( item . parent or item . name ) ) , frappe . LinkExistsError )
raises excption if the given doc is linked in another record .