Saving Contact $bean deletes email addresses

In the after_save logic hook for Accounts I wrote some code that would change the primary address of all Contacts linked to that account to the billing address of that account.

The code works, but we eventually noticed, the the mystery of vanishing email-addresses was directly related to that code I wrote:

$bean->load_relationship('contacts');
foreach ($bean->contacts->getBeans() as $contact){
   ...
  $contact->primary_address_street = $bean->billing_address_street;
  ...
  $contact->save();
}

The intended changes appear, but all linked contacts lose their email addresses.


Any ideas on how to convince $contact->save() to indeed save everything?
Parents
  • Instead of using the getBeans method use get()

    $bean->load_relationship('contacts');
    foreach ($bean->contacts->get() as $contact_id){       $contact = BeanFactory::getBean('Contacts', $contact_id);    if (! empty($contact->id)) {
        $contact->primary_address_street = $bean->billing_address_street;
        ...
        $contact->save();    }
    }

    That should resolve the issue. 
  • Hi All,

    I just came across this issue today and I was trying to figure out where the email address is being overridden.

    I found the below code under include/SugarObjects/templates/Person/Person.php under save method (checked in sugar pro 792).  You can find similar snippet under include/SugarObjects/templates/Company/Company.php as well.

    $ori_in_workflow = empty($this->in_workflow) ? false : true;
    $this->emailAddress->handleLegacySave($this, $this->module_dir);
    // bug #39188 - store emails state before workflow make any changes
    $this->emailAddress->stash($this->id, $this->module_dir);
    parent::save($check_notify);
    $override_email = array();
    if (!empty($this->email1_set_in_workflow)) {
        $override_email['emailAddress0'] = $this->email1_set_in_workflow;
    }
    if (!empty($this->email2_set_in_workflow)) {
        $override_email['emailAddress1'] = $this->email2_set_in_workflow;
    }
    if (!isset($this->in_workflow)) {
        $this->in_workflow = false;
    }

    if ($ori_in_workflow === false || !empty($override_email)) {
        $this->emailAddress->save($this->id, $this->module_dir, $override_email, '', '', '', '', $this->in_workflow);
    }
    I change the above condition (in italic) to below and the issue got fixed.

    if ($ori_in_workflow && !empty($override_email)) {
        $this->emailAddress->save($this->id, $this->module_dir, $override_email, '', '', '', '', $this->in_workflow);
    }

    $this->in_workflow   will be set to true if the bean is being dealt with in a workflow
    I have not tested the impact when bean is involved in workflow, but I feel the condition was written wrongly which was causing the issue.

    I know it's an old thread. But just thought of sharing what I found

    Thanks!
Reply
  • Hi All,

    I just came across this issue today and I was trying to figure out where the email address is being overridden.

    I found the below code under include/SugarObjects/templates/Person/Person.php under save method (checked in sugar pro 792).  You can find similar snippet under include/SugarObjects/templates/Company/Company.php as well.

    $ori_in_workflow = empty($this->in_workflow) ? false : true;
    $this->emailAddress->handleLegacySave($this, $this->module_dir);
    // bug #39188 - store emails state before workflow make any changes
    $this->emailAddress->stash($this->id, $this->module_dir);
    parent::save($check_notify);
    $override_email = array();
    if (!empty($this->email1_set_in_workflow)) {
        $override_email['emailAddress0'] = $this->email1_set_in_workflow;
    }
    if (!empty($this->email2_set_in_workflow)) {
        $override_email['emailAddress1'] = $this->email2_set_in_workflow;
    }
    if (!isset($this->in_workflow)) {
        $this->in_workflow = false;
    }

    if ($ori_in_workflow === false || !empty($override_email)) {
        $this->emailAddress->save($this->id, $this->module_dir, $override_email, '', '', '', '', $this->in_workflow);
    }
    I change the above condition (in italic) to below and the issue got fixed.

    if ($ori_in_workflow && !empty($override_email)) {
        $this->emailAddress->save($this->id, $this->module_dir, $override_email, '', '', '', '', $this->in_workflow);
    }

    $this->in_workflow   will be set to true if the bean is being dealt with in a workflow
    I have not tested the impact when bean is involved in workflow, but I feel the condition was written wrongly which was causing the issue.

    I know it's an old thread. But just thought of sharing what I found

    Thanks!
Children
No Data