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. 
  • Solution (hack) for Sugar7 :

    $bean_contact->emailAddress->handleLegacyRetrieve($bean_contact);
    $adresses = $bean_contact->emailAddress->getAddressesForBean($bean_contact);
    $contact_email = $adresses[0]["email_address"];
    $bean_contact->save();
    $bean_contact->emailAddress->addAddress($contact_email,true,false,false,false,null);
    $bean_contact->emailAddress->save($bean_contact->id, "Contacts");

    Get the email, save the bean, Set and save the email.
Reply
  • Solution (hack) for Sugar7 :

    $bean_contact->emailAddress->handleLegacyRetrieve($bean_contact);
    $adresses = $bean_contact->emailAddress->getAddressesForBean($bean_contact);
    $contact_email = $adresses[0]["email_address"];
    $bean_contact->save();
    $bean_contact->emailAddress->addAddress($contact_email,true,false,false,false,null);
    $bean_contact->emailAddress->save($bean_contact->id, "Contacts");

    Get the email, save the bean, Set and save the email.
Children
No Data