I run an instance of SuiteCRM v7.7
- OS: CENT OS
- PHP Version: 5.5.36
- DB: MySQL v5.5.50
I've written a scheduled job that, every set interval, it will access Leads records and compare two fields with each other (account_id
and account_id_c
). It makes sure that they are both equal.
Upon import of a list of leads, these fields should be empty and when the job runs, it'll find the records with empty account_id
and account_id_c
fields and then find the IDs for the account_name
listed in the record. Once the account is located, it inserts the account ids into account_id
and account_id_c
fields.
Once the fields match, the bean is saved, and so it goes.
Here's a look at my scripting for the scheduled job:
$job_strings[] = 'checkCurrentLeadCompany';
function checkCurrentLeadCompany(){
$bean = BeanFactory::getBean('Leads');
$order_by = "last_name";
$where = "leads.account_id IS NULL OR leads.account_id = ''";
$importedLeadList = $bean ->get_full_list($order_by,$where);
function importedLeads($importedLeadList){
foreach ($importedLeadList as $record) {
$accountBean = BeanFactory :: getBean('Accounts');
$accountRecord = $accountBean -> retrieve_by_string_fields(
array(
'name' => $record -> account_name
)
);
if(isset($accountRecord) || $accountRecord != NULL || $accountRecord != ''){//checks to see if a record populates the variable
$record -> account_id = $accountRecord -> id;
$record -> account_id_c = $accountRecord -> id;
}
else{
//If there is no account available, create the account and proceed.
$newRecord = createAccount($record -> account_name);
$record -> account_id = $newRecord -> id;
$record -> account_id_c = $newRecord -> id;
}
$record->save();
}
}
function createAccount($accountName, $content){
$newAccount = BeanFactory :: newBean('Accounts');
$newAccount -> name = $accountName;
$newAccount ->save();
return $newAccount;
}
if(!isset($importedLeadList)){
//no need to do anything.
}
else{
importedLeads($importedLeadList);
}
return true;
}
The funny thing: every time this script runs, the relationship between the lead record and its email address gets broken. In the database, the "deleted
" field in the linking table, email_addr_bean_rel
, gets set to 1. I don't know why.
Can anybody give me any insight as to why this happens?