Can't Add Primary Email Address in Leads

Hello,

I’m just trying to create a new lead, and all works well except the primary email address. My code as below:

$lead = new Lead();
$lead->account_name = ‘Demo Lead Account Name’;
$lead->first_name = ‘Fred’;
$lead->last_name = ‘Derf’;
$lead->primary_address_street = ‘123 Church Street’;
$lead->primary_address_city = ‘Winder’;
$lead->title = ‘Mr.’;
$lead->phone_work = ‘678 555-1212’;
$lead->phone_home = ‘678 555-1222’;
$lead->phone_mobile = ‘678 555-1223’;
$lead->emailAddress->addAddress(‘fred@derf.com’, true); // this doesn’t happen, email is blank
$lead->primary_address_state = ‘GA’;
$lead->status = ‘New’;
$lead->primary_address_postalcode = 30380;
$lead->primary_address_country = ‘US’;
$lead->save();

This was cribbed almost exactly from install/populateSeedData.php file. Any help/pointers would be appreciated.

Did you check your logfiles (both php and sugarcrm)? Possibly you could find the problem in there.

Does your script include the file: include/SugarEmailAddress/SugarEmailAddress.php (it may be that it is already included somewhere in fact)
(this is the file where the method AddAddress is defined at around 528 as:
function addAddress($addr, $primary=false, $replyTo=false, $invalid=false, $optOut=false, $email_id = null)

I also found that there is another method with the same name (AddAddress) which is defined in another class that you can find here: include/phpmailer/class.phpmailer.php
I doubt that this is the problem though

amariussi,

There’s nothing in the apache error_log, or the suitecrm.log file. Yes, the method’s calling the add_address properly, I do a print_r on $this_addresses inside the function right before the return and I see:
Array ( [0] => Array ( [email_address] => fred@derf.com [primary_address] => 1 [reply_to_address] => 0 [invalid_email] => 0 [opt_out] => 0 [email_address_id] => ) )
There’s no corresponding row inserted into the email_addresses table, so it looks to me like the lead->save() function isn’t saving it properly.

Any ideas? Thanks,

Brent Laminack

I notice the exact same thing and here’s some more on this…

When I create a new lead from PHP (e.g. via logic hook or scheduler script) using something like;

$sea = new SugarEmailAddress;
$sea->addAddress('myemail@host.com', true);
$sea->save($record_id, "Leads");

Here’s what happen;

  1. It works; the email is indeed created
  2. BUT in the email_addr_bean_rel table, the relationship between the leads table and the email_addresses table is broken because [color=#ff0000]email_addr_bean_rel.deleted=1[/color]

If i use the same code on an existing lead, all works fine.

So IMHO the culprit is somewhere in the SugarEmailAddress class where it somehow marks the deletion in the junction table if you are in the same PHP session that created the lead.

For myself, instead of trying to fix the class, I simply make an SQL query to correct the faulty value in email_addr_bean_rel.deleted right after the SugarEmailAddress::save() call.

Hope it helps!

ok - I found the problem after a few hours of fiddling around

In any circumstances, if you need to add an email address to a Lead (or any other bean kind I woud assume) you have to make sure to do so AFTER THE LAST $lead->save() call.

Given this situation;

// Create Lead
$lead = BeanFactory::newBean('Leads');
$lead->new_with_id = true;
$lead->id = create_guid();

/* some more code... */

$lead->save();

// Add email
$email = new SugarEmailAddress;
$email->addAddress('some@email.com', true);
$email->save($lead->id, "Leads"); // SO FAR SO GOOD, EMAIL IS THERE AND ALL IS NORMAL

// Add, let's say a meeting...
$meeting = BeanFactory::getBean('Meetings');
/* ... more code... */
$meeting->save();
$lead->load_relationship('meetings');
$lead->meetings->add($meeting->id);
$lead->save(); // HERE'S THE PROBLEM

The second $lead->save(); breaks the relationship between the Lead and the EmailAddress in the table email_addr_bean_rel, putting the deleted column to “1” (true).

If you do it like this, it should work;

// Create Lead
$lead = BeanFactory::newBean('Leads');
$lead->new_with_id = true;
$lead->id = create_guid();

/* some more code... */

$lead->save();

// Add a meeting...
$meeting = BeanFactory::getBean('Meetings');
/* ... more code... */
$meeting->save();
$lead->load_relationship('meetings');
$lead->meetings->add($meeting->id);
$lead->save(); 

// Lastly, add the email
$email = new SugarEmailAddress;
$email->addAddress('some@email.com', true);
$email->save($lead->id, "Leads");

Hope this will prevent many coders from pulling their hair.

Hello

try this example:
$lead = new Lead();
$lead->first_name = ‘Fred’;
$lead->last_name = ‘Derf’;
$lead->email1 = ‘fred@derf.com’;
$lead->save();

2 Likes

well… i’ll be damn… this seems to be working.

Thanks!

This also worked for me on the Contacts module – just don’t forget the $bean->save(); bit – thanks Frenchy!

I am having issues with using set_parameters_entry using REST API. The primary email address is not being set using email1 as the parameter name. Anyone else have this problem? any work around?

@consultaxion
I am having same problem adding email address in Contacts. I tried your code but still no solution.

I have the same problem on all contacts when I save, I get “Email Address(Primary) is already exist”

Version 7.11.20

Sugar Version 6.5.25 (Build 344)