diff --git a/modules/crm_core_contact/crm_core_contact.module b/modules/crm_core_contact/crm_core_contact.module
index b83f691..9ae5d1a 100644
--- a/modules/crm_core_contact/crm_core_contact.module
+++ b/modules/crm_core_contact/crm_core_contact.module
@@ -877,7 +877,9 @@ function crm_core_contact_file_download_access($file_item, $entity_type, $entity
  */
 function crm_core_contact_query_crm_core_contact_search_alter(QueryAlterableInterface $query) {
   // TODO: get table and field names automatically.
-  $table = 'field_data_contact_name';
+  $fields = $query->getFields();
+  $table = $fields['bundle']['table'];
+  $crm_core_contact_table = 'crm_core_contact';
 
   $fields = array(
     'title' => 'contact_name_title',
@@ -898,7 +900,7 @@ function crm_core_contact_query_crm_core_contact_search_alter(QueryAlterableInte
   $coalesce = implode(', ', $coalesce);
 
   $where_expression = "LOWER(CONCAT(' ', $coalesce)) LIKE $placeholder";
-  $query->innerJoin($table, $table, "crm_core_contact.contact_id = {$table}.entity_id");
+  $query->innerJoin($crm_core_contact_table, $crm_core_contact_table, "crm_core_contact.contact_id = {$table}.entity_id");
   $query->where($where_expression, array($placeholder => $value));
   // If set $query->addMetaData('user', TRUE); select contacts which have associated users, FALSE without association.
   if (isset($query->alterMetaData['user'])) {
@@ -908,7 +910,6 @@ function crm_core_contact_query_crm_core_contact_search_alter(QueryAlterableInte
     $where_cond .= empty($query->alterMetaData['user']) ? 'NULL' : 'NOT NULL';
     $query->where($where_cond);
   }
-
 }
 
 /**
