retrieve_by_string_fields has a TERRIBLE HUGE bug.
it retrieves the bean without its emails.
so if i want to search a lead by a mobile phone and update its details and do save(), i actually delete all the lead emails,
because i extracted them without emails and then saved again.
So i'm trying to restore the lead emails after i find it using retrieve_by_string_fields(), but only the email1 email is saved. all other emails - are gone, although i add them again too , to the Lead.
The lead is always saved with only one email, this is the simple code:
$lead->retrieve_by_string_fields(array('phone_mobile' => $mobilephone));
// if lead with this mobile phone number was found
if ( !empty($lead->id))
// Because retrieve_by_string_fields gets the bean without the emails
// retrieve the emails of the lead again and insert them into lead bean back again
$sea = new SugarEmailAddress;
// Grab the array of addresses
$addresses = $sea->getAddressesByGUID($lead->id, "Leads");
if ( !empty($addresses))
foreach ( $addresses as $address )
$mybeanEmails = new SugarEmailAddress;
$theaddress = $address['email_address'];
// if primary , then insert as primary
if ( $address['primary_address'] == 1)
// Add a primary email address
$lead->email1 = $theaddress;
i'm using sugarcrm CE 6.5.13