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

Fullscreen
1
2
$contactBean->emailAddress->addAddress($new_email_address, true);
$contactBean->save();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

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

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

  • I have not been able to get that to work either. All the addAddress does is add it to the array of Email Addresses for the record, but it never gets saved to the database.

    I ended up with the following which is not pretty, but works:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $emailParams = array(
    'email' => $sugarObject->emailAddress->addresses,
    );
    $emailParams['email'][] = array(
    'email_address' => $remoteObject->$remoteFieldName,
    'primary_address' => (count($emailParams['email']) == 0 ? 1 : 0),
    'opt_out' => !empty($GLOBALS['sugar_config']['new_email_addresses_opted_out']),
    );
    $sugarFieldEmail = new SugarFieldEmail("email");
    $sugarFieldEmail->apiSave($sugarObject, $emailParams, "email", $sugarFieldDefs['email']);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Thanks for the workaround, I filed a case with Sugar Support, I'm surprised I could not find a bug on file for this. It seems like a pretty big issue... 

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

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

    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:

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

    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

  • Hi Francesca,

    I'll take this to our engineering and PMs and will circle back to you on this.

    Keep you posted.

    rafa

    SugarCRM | Principal Developer Advocate

  • You didn't miss something because I can't find it either. I just did some testing myself and simply following the documentation would indeed lead to nowhere. The code that seems to work, and doesn't break anything, is as follows: (for adding addresses). But  removing doesn't seem to be working :( 

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    // load the bean we want to update.
    $c = BeanFactory::retrieveBean('Contacts','f8c35990-453b-11eb-8f2d-34363bc92236');
    // add sample email adress to this bean
    $c->emailAddress->addAddress('address@sugar.crm');
    // save the email relationship
    $c->emailAddress->save($c->id, $c->module_dir);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • So you would set the primary when you add the address but then instead of saving the Contact bean you would specifically save the emaiLAddress on the Contact bean. 

    Seems reasonable (though as established, not the documented path).

  • I tried to set the primary... it works, it didn't delete anything... but now I have TWO primary addresses!!

    Fullscreen
    1
    2
    3
    4
    5
    if(!empty($contactBean)){
    //add the new address as primary
    $contactBean->emailAddress->addAddress($new_email_address, 1);
    $contactBean->emailAddress->save($contactBean->id, $contactBean->module_dir);
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • that's odd. But from the code point of view 'correct'. I just checked the code again and htere is an function to update an existing e-mail address but you can't set the primary flag. I have to play with this a bit more to see what can be done.. 

1 2