def get_default_address(doctype, name, sort_key=u'is_primary_address'):
 out = frappe.db.sql(u'select\n DCTB  DCTB  DCTB parent,\n DCTB  DCTB  DCTB (select `{0}` from tabAddress a where a.name=dl.parent) as `{0}`\n DCTB  DCTB from\n DCTB  DCTB  DCTB `tabDynamic Link` dl\n DCTB  DCTB where\n DCTB  DCTB  DCTB link_doctype=%s and\n DCTB  DCTB  DCTB link_name=%s and\n DCTB  DCTB  DCTB parenttype = "Address"\n DCTB  DCTB '.format(sort_key), (doctype, name))
 if out:
  return sorted(out, (lambda x, y: cmp(y[1], x[1])))[0][0]
 else:
  return None

def get_default_address ( doctype , name , sort_key = u'is_primary_address' ) : out = frappe . db . sql ( u'select\n DCTB  DCTB  DCTB parent,\n DCTB  DCTB  DCTB (select `{0}` from tabAddress a where a.name=dl.parent) as `{0}`\n DCTB  DCTB from\n DCTB  DCTB  DCTB `tabDynamic Link` dl\n DCTB  DCTB where\n DCTB  DCTB  DCTB link_doctype=%s and\n DCTB  DCTB  DCTB link_name=%s and\n DCTB  DCTB  DCTB parenttype = "Address"\n DCTB  DCTB ' . format ( sort_key ) , ( doctype , name ) ) if out : return sorted ( out , ( lambda x , y : cmp ( y [ 1 ] , x [ 1 ] ) ) ) [ 0 ] [ 0 ] else : return None
returns default address name for the given doctype .