Contact lost email when save it

Hi,

I create a hook on Account for update date modification of all related contact

Like this:


$contacts = $bean->get_linked_beans('contacts','Contact');
foreach ( $contacts as $contact ) {
    $contact->save();
}

But when I save the contact, I lose email address for eatch contact

Someone can tell me why and how to fix the problem if possible

Thank you

Read this for a background

https://pgorod.github.io/How-Emails-Stored/

But that is explained in database terms. With the beans I don’t know exactly, you need to look around for that relationship so you can copy it also.

If you get a working piece of code please come back here and share, I might even put it up on that blog if you permit. :slight_smile:

Hi

I know how email address was stored inside data base

But is very weird that you lose email adress

I have the same effect if I use this code


$updated_contact = new Contact();
$updated_contact->retrieve($contact->id);

Anybody can help my

it is because I need to get contact email before call method save ?

I don’t know how to help you… but scanning the Contacts bean I see these fields in there:

id
name
date_entered
date_modified
modified_user_id
modified_by_name
created_by
created_by_name
description
deleted
created_by_link
modified_user_link
assigned_user_id
assigned_user_name
assigned_user_link
SecurityGroups
salutation
first_name
last_name
full_name
title
photo
department
do_not_call
phone_home
email
phone_mobile
phone_work
phone_other
phone_fax
email1
email2

invalid_email
email_opt_out
primary_address_street
primary_address_street_2
primary_address_street_3
primary_address_city
primary_address_state
primary_address_postalcode
primary_address_country
alt_address_street
alt_address_street_2
alt_address_street_3
alt_address_city
alt_address_state
alt_address_postalcode
alt_address_country
assistant
assistant_phone
email_addresses_primary
email_addresses
email_addresses_non_primary
email_and_name1

lead_source
account_name
account_id
opportunity_role_fields
opportunity_role_id
opportunity_role
reports_to_id
report_to_name
birthdate
accounts
reports_to_link
opportunities
bugs
calls
cases
direct_reports
emails
documents
leads
meetings
notes
project
project_resource
tasks
tasks_parent
notes_parent
user_sync
campaign_id
campaign_name
campaigns
campaign_contacts
c_accept_status_fields
m_accept_status_fields
accept_status_id
accept_status_name
prospect_lists
sync_contact
fp_events_contacts
aos_quotes
aos_invoices
aos_contracts
e_invite_status_fields
event_status_name
event_invite_id
e_accept_status_fields
event_accept_status
event_status_id
project_contacts_1
aop_case_updates
joomla_account_id
portal_account_disabled
joomla_account_access
portal_user_type

… as you can see there are plenty of different email related fields. Try looking at those values to see what is filled, and what is not. Maybe you have to load some relationship before saaving the bean, otherwise it won’t be filled…

But this might just be a bug, I’m afraid…

When I test this


$contacts = $account->get_linked_beans('contacts','Contact');
foreach ( $contacts as $contact ) {
    $GLOBALS['log']->info('email: '.$contact->email);

    $tmp_contact = new Contact();
    $tmp_contact->retrieve($contact->id);

    $GLOBALS['log']->info('email: '.$tmp_contact->email);
}

The fields email is always empty

It is normal ?? I don’t think so

I would just try printing out ALL of them and see where the value comes

email
email1
email2
email_addresses_primary
email_addresses
email_addresses_non_primary
email_and_name1

as long as the value is there in one place…

And you better use print_r instead of just trying to print as a string… these could be arrays.

I try it and only the field email1 have data

I also have an import script where I used the email1 field to fill in the email. I don’t remember why, I probably just tried it until it worked.

I think this thread here has some explanation about what’s happening to you:

https://suitecrm.com/forum/developer-help/6754-can-t-add-primary-email-address-in-leads

although I’m not sure any of those solutions will be enough for you.

Pay attention to the “email_addr_bean_rel” table, see if your addresses are there with “deleted=1”

If so, that is probably happening in this line

https://github.com/salesagility/SuiteCRM/blob/master/include/SugarEmailAddress/SugarEmailAddress.php#L291

which would be a bug, of course. You could try commenting the line below that line (the query command) temporarily and trying your code to see if the addresses are preserved. But then don’t leave it like that, this is just for diagnostic, then we’d have to come up with a proper fix.