Contact $bean->save() deletes related email address

HI all,

I have an unexpected behaviour:

I have an API Entry Point, where a contact and its email address is saved at the database. Below some simplified source code:

Fullscreen
1
2
3
4
5
6
7
8
$bean = BeanFactory::newBean("Contacts");
$bean->last_name = $last_name;
$bean->status = "New";
$bean->save();
$sea = new SugarEmailAddress();
$sea->addAddress($email, true, false, false, false);
$sea->save($bean->id, "Contacts");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Then some logic is done and in some cases the contact need to be adjusted after that, so the contact is loaded with getBean and saved:

Fullscreen
1
2
3
4
5
$bean_id = 'TheIDoftheSavedContactFromAbove!';
$bean2 = BeanFactory::getBean("Contacts", $bean_id);
$bean2->a_example_field = "Test entry";
$bean2->save();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

After that $bean2->save(), the relation of the Contact to the Email address is marked as deleted=1

How to stop this strange behaviour.

Many thanks

Best Regards

Sven