I added contacts directly to database but cannot find contact with suiteCRM search

I tried to just add everything I needed using API calls but it mangled an account number that I need intact. Couldn’t figure an easy way to fix that so I went the route of adding contacts via mysql. Now I’ve added a contact that I cannot find with the search feature… trying to figure out what I’m missing. Another table? Is there a cache to clear & rebuild? Yes, I’ve tried the basic repair to no avail

The Global search is indexed. If you added from the database, no indexing occurred.

If it’s only one, just access the record (you can use the id you have in the database, pass it in the URL of the Detail view), and edit it, and save. This should trigger the indexing.

If this doesn’t work, or if you have many new records, consider a full reindex of the AOD index: https://pgorod.github.io/Reindex-AOD/

1 Like

you have to create the record in aod_indexevent. That is the table used with the global search.

I am not sure what the aod_indexevent table does, without the other data structures in the Index (see directory modules/AOD_Index/Index/Index)… I think both are necessary?

the index file is autogenerated, and it takes information from aod_indexevent table. Everytime a new record is created, the aod_indexevent table get popolated with the information of this new record, because this is the table that uses global search. So if he want to this records appears in the global, he has to insert the record in this table too. I know that because i got a similar problem and i solved by create a script. Btw the best solution to insert data in the db is by using api or create a script that run by entrypoint and uses Beans. Because if you use beans to insert, the index is inserted automaticaly

1 Like

@GianlucaSedoc

Could you share your script with a brief explanation?

[quote=“amariussi” post=75784]@GianlucaSedoc


<?php
include "db_connect.php";



$errore = "inizio alle ore: ".date("H:i:s");
file_put_contents(__DIR__."\\log\\log_indexes\\logIndex.txt",$errore."\n",FILE_APPEND);
$count=0;

$query_account_id = "SELECT id,name FROM accounts where deleted=0 ";
$query_id = sqlsrv_query($connectionInfo_CRMSuite,$query_account_id);
while ($row_id = sqlsrv_fetch_array($query_id,SQLSRV_FETCH_ASSOC)){
    $name_value_list = [];
    $account_id = $row_id['id'];
    $checkQuerty= "SELECT id FROM aod_indexevent where record_id='$account_id'";
    $checkQuerty = sqlsrv_query($connectionInfo_CRMSuite,$checkQuerty);
    $checkQuerty = sqlsrv_fetch_array($checkQuerty,SQLSRV_FETCH_ASSOC);
    if(empty($checkQuerty)){
		try{
			$name_value_list['id'] = $account_id;
			$indexEvent = BeanFactory::newBean("AOD_IndexEvent");
			$indexEvent->record_id = $account_id;
			$indexEvent->name = iconv('','UTF-8',$row_id['name']);
			$indexEvent->record_module = 'Accounts';
			$indexEvent->update_date_modified = false;
			$indexEvent->date_modified = date("Y-m-d H:i:s");
			$indexEvent->success = true;
			$indexEvent->save();
		}catch(Exception $ex){
			$errore = $ex->getMessage();
			file_put_contents(__DIR__."\\log\\log_indexes\\logIndex_errore.txt",$errore."\n",FILE_APPEND);
        }
    }
}

$query_contacts_id = "SELECT id,first_name,last_name FROM contacts where deleted=0 ";
$query_id = sqlsrv_query($connectionInfo_CRMSuite,$query_contacts_id);
while ($row_id = sqlsrv_fetch_array($query_id,SQLSRV_FETCH_ASSOC)){
    $contact_id = $row_id['id'];
    $checkQuerty= "SELECT id FROM aod_indexevent where record_id='$contact_id'";
    $checkQuerty = sqlsrv_query($connectionInfo_CRMSuite,$checkQuerty);
    $checkQuerty = sqlsrv_fetch_array($checkQuerty,SQLSRV_FETCH_ASSOC);
    if(empty($checkQuerty)){
        try{
            $indexEvent = BeanFactory::newBean("AOD_IndexEvent");
            $indexEvent->record_id = $contact_id;
            $indexEvent->name = iconv('','UTF-8',$row_id['first_name'])." ".iconv('','UTF-8',$row_id['last_name']);
            $indexEvent->record_module = 'Contacts';
            $indexEvent->update_date_modified = false;
            $indexEvent->date_modified = date("Y-m-d H:i:s");
            $indexEvent->success = true;
            $indexEvent->save();
        }catch(Exception $ex){
            $errore = $ex->getMessage();
			file_put_contents(__DIR__."\\log\\log_indexes\\logIndex_errore.txt",$errore."\n",FILE_APPEND);
        }
    }
}



$query_contacts_id = "SELECT id,first_name,last_name FROM leads where deleted=0 ";
$query_id = sqlsrv_query($connectionInfo_CRMSuite,$query_contacts_id);
while ($row_id = sqlsrv_fetch_array($query_id,SQLSRV_FETCH_ASSOC)){
    $lead_id = $row_id['id'];
    $checkQuerty= "SELECT id FROM aod_indexevent where record_id='$lead_id'";
    $checkQuerty = sqlsrv_query($connectionInfo_CRMSuite,$checkQuerty);
    $checkQuerty = sqlsrv_fetch_array($checkQuerty,SQLSRV_FETCH_ASSOC);
    if(empty($checkQuerty)){
        try{
            $indexEvent = BeanFactory::newBean("AOD_IndexEvent");
            $indexEvent->record_id = $lead_id;
            $indexEvent->name = iconv('','UTF-8',$row_id['first_name'])." ".iconv('','UTF-8',$row_id['last_name']);
            $indexEvent->record_module = 'Contacts';
            $indexEvent->update_date_modified = false;
            $indexEvent->date_modified = date("Y-m-d H:i:s");
            $indexEvent->success = true;
            $indexEvent->save();
        }catch(Exception $ex){
            $errore = $ex->getMessage();
			file_put_contents(__DIR__."\\log\\log_indexes\\logIndex_errore.txt",$errore."\n",FILE_APPEND);
        }
    }
}


$errore = "Fine alle ore: ".date("H:i:s");
file_put_contents(__DIR__."\\log\\log_indexes\\logIndex.txt",$errore."\n",FILE_APPEND);



the first include is the connection to the database.

This script works with accounts and contacts and i used to fix an insert directly from db.

If you instead want to insert account from like a csv, i’ve made this class:



<?php
/**
 * Created by PhpStorm.
 * User: Francesco
 * Date: 14/08/2018
 * Time: 15:09
 */

class Import_class {

    private $basedir = "C:\\workspace\\GammaCRM\\custom\\service\\schedulers\\csv";

    public function AccountImport($inizio,$fine){

        if (($handle = fopen($this->basedir."\\Aziende.csv", "r")) !== FALSE) {

            $row=0;
            $AccountBean = BeanFactory::getBean('Accounts');
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {


                if($row>=$inizio && $row<=$fine) {

                    $id_csv_user = $data[22];
                    $UsersBean =  BeanFactory::getBean('Users');
                    $user = $UsersBean->retrieve_by_string_fields(array('id_csv_c'=>$id_csv_user,'deleted'=>'0'));

                    $id_csv = $data[0];
                    if($id_csv=="")
                        continue;
                    $account=$AccountBean->retrieve_by_string_fields(array('id_csv_c'=>$id_csv,'deleted'=>'0'));

                    if(empty($account))
                        $account = BeanFactory::newBean("Accounts");



                    $account->id_csv_c                      = $data[0];
                    $account->id_sap_c                      = $data[1];
                    $account->name                          = $data[2];
                    $account->website                       = $data[3];
                    $account->email1                        = $data[4];
                    $account->email_non_principale_c        = $data[5];
                    $account->phone_office                  = $data[6];
                    $account->phone_alternate               = $data[7];
                    $account->phone_fax                     = $data[8];
                    $account->billing_address_street        = $data[9];
                    $account->billing_address_city          = $data[10];
                    $account->billing_address_state         = $data[11];
                    $account->billing_address_postalcode    = $data[12];
                    $account->billing_address_country       = $data[13];
                    $account->shipping_address_street       = $data[14];
                    $account->shipping_address_city         = $data[15];
                    $account->shipping_address_state        = $data[16];
                    $account->shipping_address_postalcode   = $data[17];
                    $account->shipping_address_contry       = $data[18];
                    $account->account_type                  = trim(strtolower($data[19]));
                    $account->industry                      = $data[20];
                    $account->description                   = $data[21];
                    $account->assigned_user_id              = $user->id;
                    $account->agente_di_riferimento_c       = $data[23];
                    $account->partita_iva_c                 = $data[24];
                    $account->priorita_assegnata_c          = $data[25];
                    $account->employees                     = $data[26];
                    $account->annual_revenue                = $data[27];
                    $account->modified_user_id              = 1;
                    $account->created_by                    = 1;

                    $account->save();
                }
                $row++;
            }
        }
    }

    public function ContactsImport($inizio,$fine){
        if (($handle = fopen($this->basedir."\\Contatti.csv", "r")) !== FALSE) {
            $row=0;
            $ContactsBean = BeanFactory::getBean('Contacts');
            while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
                if($row>$inizio && $row<$fine) {
                    $id_csv = $data[0];
                    if($id_csv=="")
                        continue;

                    $id_csv_account = $data[3];
                    $AccountBean =  BeanFactory::getBean('Accounts');
                    $account = $AccountBean->retrieve_by_string_fields(array('id_csv_c'=>$id_csv_account,'deleted'=>'0'));
                    if(!empty($account))
                        $assigned_user_id = $account->assigned_user_id;
                    else
                        $assigned_user_id = 1;

                    $contact=$ContactsBean->retrieve_by_string_fields(array('id_csv_c'=>$id_csv,'deleted'=>'0'));

                    if(empty($contact))
                        $contact = BeanFactory::newBean("Contacts");

                    $contact->id_csv_c              = $data[0];
                    $contact->first_name            = $data[1];
                    $contact->last_name             = $data[2];
                    $contact->title                 = $data[4];
                    $contact->email1                = $data[6];
                    $contact->email_alternativa_c   = $data[7];
                    $contact->phone_mobile          = $data[8];
                    $contact->phone_work            = $data[9];
                    $contact->assigned_user_id      = $assigned_user_id;
                    $contact->created_by            = 1;
                    $contact->modified_user_id      = 1;
                    $contact->save();
                    $contact_id = $contact->id;
                    if(!empty($account)){
                        $account->load_relationship('contacts');
                        $account->contacts->add($contact_id);
                        $account->save();
                    }

                }
                $row++;
            }
        }
    }



}


care because i edited to hide some other function and some } could be missing

2 Likes