3

I run an instance of SuiteCRM v7.7

  1. OS: CENT OS
  2. PHP Version: 5.5.36
  3. 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?

Star
  • 3,222
  • 5
  • 32
  • 48

1 Answers1

2

I came across the same problem with Contacts.

After hours of trying to stash the email addresses before saving the contact and then re-link them to the contact (which didn't work) I took a look at the 'Person' class that 'Contact' extends.

The 'save' method looks for the existence of a GLOBAL var called 'resavingRelatedBeans'. If you set this just before saving the Contact bean (remember to unset it after) it doesn't mess up the currently associated email addresses. So now I have:

 $GLOBALS['resavingRelatedBeans']=true;
 $contactRec->save();
 $GLOBALS['resavingRelatedBeans']=null;
glidester
  • 599
  • 6
  • 23
  • Life Saver, for me this problem was occurring on any related contact that had more than 2 email addresses, it was deleting the 3rd email for some bizarre reason. – Josh Whitlow Oct 31 '17 at 21:30