$bean->emailAddress->addAddress is not working, and $sea->addAddress REMOVES all other addresses

I am writing an API to change an email address on a contact.
I don't want to remove the old address, just add the new one as primary on the Contact bean.

I used the documented addAddress ( support.sugarcrm.com/.../ )

$contactBean->emailAddress->addAddress($new_email_address, true);
$contactBean->save();

But this won't even add the new address to Contact at all. There are no errors in the logs, I verified that the contactBean is a valid Bean, and that the $new_email_address is a valid address. I checked the parameters for the addAddress function and I verified that the function is being called.

What's worse

$sea = new SugarEmailAddress();
      $sea->addAddress($email_address,true);
      //do some checks...
      //add the email address to the Contact $bean
      $sea->save($bean->id, 'Contacts');

Adds the new address but DELETES all the others!

Has something changed in v11.0.1 (on site Professional) that I'm not aware of?

Thank you,
Francesca

Parents
  • I found another issue... in some scripts I was using:

          $sea = new SugarEmailAddress();
          $sea->addAddress($email_address,true);
          //do some checks...
          //add the email address to the Lead $bean
          $sea->save($bean->id, 'Leads');
          
    

    While this works, it is now deleting ALL other addresses.

  • That is odd. Maybe you already checked it yourself in the core files but it is due to the way how the SugarEmailAddress class works. By default it does initialize the internal address array empty. When you directly add addresses to that using the addAddress function you actually adding them directly to that empty array. When you execute the save method it will then not validate what the other addresses are because it does not know yet which bean it is related to. See the following lines i the save method of that class:

    if (empty($this->addresses) || $in_workflow) {
                $this->populateAddresses($id, $module, $new_addrs, $primary);
            }

    If you call the populateAddressess before you add a new address to it you should be fine, I think. 

    btw the code for this class hasn't been changed much between 10.0.3 and 11.0.1 atleast not for this functionality.

Reply
  • That is odd. Maybe you already checked it yourself in the core files but it is due to the way how the SugarEmailAddress class works. By default it does initialize the internal address array empty. When you directly add addresses to that using the addAddress function you actually adding them directly to that empty array. When you execute the save method it will then not validate what the other addresses are because it does not know yet which bean it is related to. See the following lines i the save method of that class:

    if (empty($this->addresses) || $in_workflow) {
                $this->populateAddresses($id, $module, $new_addrs, $primary);
            }

    If you call the populateAddressess before you add a new address to it you should be fine, I think. 

    btw the code for this class hasn't been changed much between 10.0.3 and 11.0.1 atleast not for this functionality.

Children