$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.

  • We jumped from 9.x to 11.x and I didn't see anything in the Release Notes for any of the versions in between (I checked them all) about such a major change in functionality. Did I miss something?

    What puzzles me is that the documentation says nothing about this:

    It talks about ADDING addresses not, REPLACING addresses:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_11.0/Architecture/Email_Addresses/#Updating_Email_Addresses_Using_the_SugarBean

    I am very confused... it was not behaving this way before, but it doesn't look like this particular file changed between v9 and v11.

    FrancescaS

Reply Children