AnsweredAssumed Answered

after_save logic hook causing duplicate sql insert error for leads

Question asked by sonesay inthavong on Jul 2, 2015
Latest reply on Jul 2, 2015 by sonesay inthavong
Hi All

I've been working with an after_save logic hook for a while and it all seems OK until I tested creating a new Lead record through the UI. I get an sql error where there is an insert trying to run for leads and leads_cstm table complaining about duplicate keys in the logs. This problem does not occur when new Leads come through from marketo.




When the error occurs email address gets lost. Any ideas how I can resolve this?


public function after_save($bean, $event, $arguments)
    {
        require_once '/usr/local/bin/phpObjectRecorder.php';
        $time = date("Y-m-d H:i:s");
        wo("@ $time - $this->version", false);

        wo(__METHOD__ . " ( $bean->module_name : $bean->mkto_id | " . $bean->id . " )");
//        wo('Marketo ID : ' . $bean->mkto_id);
//        $primary_email = $bean->emailAddress->getPrimaryAddress();
        wo($bean->first_name . ' ' . $bean->last_name . ' ' . $bean->email1);
        wo($event);
        wo($arguments);

        if ($this->debug) {
            if (!in_array($bean->id, $this->debug_id_list)) {
                return true;
            }
        } else {
            wo('[ RUNNING... ]');
        }

//        wo('$bean->ignore_update_c : ' . $bean->ignore_update_c); // PHP Notice !
        if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false) {

            $bean->ignore_update_c = true;

            if ($bean->current_customer_c == 1 && $bean->converted == false && $bean->module_name == 'Leads') {

                wo('[ CURRENT CUSTOMER : 1 ]');
                // include/externalAPI/Marketo/LogicHookController.php
                $contact_id = $this->ConvertLeadToContact($bean->id);
                $bean->converted = true;

                $bean->contact_id = $contact_id; // Needs to be set\

                if ($bean->downloaded_marketo_data['company'] != '') {
                    $account_id = $this->CreateAccount($bean->downloaded_marketo_data, $contact_id);
                    $bean->account_id = $account_id;
                }
            } else {
                wo('[ CURRENT CUSTOMER : 0 ]');
            }

            $this->readProductOwnedFromMarketo($bean);

            $bean->save();
        }
    }

Outcomes