This is an issue I’ve run into a few times using SugarCRM logic hooks for Accounts, Contacts and Leads records. Using a logic hook, if you update the Account’s parent/child accounts, you’ll end up copying the email addresses from the $bean you’re
working with to those that are being updated via code.
This would also happen if updating a Contact record updated other Contacts (via a custom relationship, or perhaps you update other Contacts belonging to the same Account).
The reason is in the AccountFormBase and ContactFormBase’s use of the SugarEmailAddress class’s method handleLegacyRetrieve. This code loads the email addresses for the record from $_REQUEST, which is great if it is the record you’re working with. But if you’re working with another record of the same type, $_REQUEST is filled with the first object’s data, including email addresses.
The way to avoid this is simple, but feels dirty to me. We simply store the entire $_REQUEST array, unset it so that it’s empty and handleLegacyRetrieve has nothing to work with. Before existing the logic hook, set $_REQUEST back the way it was. Here’s a quick example of an Account logic hook that will update it’s Member Of (parent) Account:
Update March 18 2013: Accidently deleted original pastebin entry, so I re-wrote the example.