AnsweredAssumed Answered

Add email address through code : Does not work for Accounts module

Question asked by Gaelle Fernandez on Jul 28, 2015
Latest reply on Jul 29, 2015 by Gaelle Fernandez
Hello everyone !

Working on Sugar 7.5.2.1, i've come to a little problem..

I'm trying to add email addresses to some Contacts and some Accounts.

So i've followed this link (i know, it is an oldie), and it works for my Contacts.

Here is the code of my Contacts : i have an update of an existing Contacts and i have a creation of a new Contact.
The updated one :

$bean2->retrieve_by_string_fields(array('num_client_c' => $raw_data[30]));
$bean2->first_name = $raw_data[0];
$bean2->canal_c = $raw_data[48]; 
$bean2->is_vip_c = $raw_data[50];
// Ajout de l'adresse email primaire
$sea = new SugarEmailAddress;
if ($sea->getPrimaryAddress($bean2) == null || $sea->getPrimaryAddress($bean2) == "" || strlen($sea->getPrimaryAddress($bean2)) === 0 ){ 
  $sea->addAddress($raw_data[52], true); 
  $sea->save($bean2->id, "Contacts");
}

The new one :

$bean4 = new Contact();   if(($raw_data[30] != "" || strlen($raw_data[30])>0) && $bean4->retrieve_by_string_fields(array('num_client_c' => $raw_data[30])) == null )
{
  echo "création";
  //on ne le trouve pas en base : création
  $bean4->first_name = $raw_data[0];
  $bean4->last_name = $raw_data[1];                  $bean4->canal_c = $raw_data[48];
                $bean4->is_vip_c = $raw_data[50];
               $pp_id = $bean4->save(); 
                 // Ajout de l'adresse email primaire
               $sea = new SugarEmailAddress;
               $sea->addAddress($raw_data[52], true); 
               $sea->save($pp_id, "Contacts");  }
                                          And these codes work for Contacts.

But these SAME snippets of code do not work for Accounts.
I've written exactly the same, only replacing  $sea->save($pp_id, "Contacts"); by  $sea->save($pp_id, "Accounts");

I've tested, my $raw_data[52] does contain an email address. And more : the email address has been registered in the table email_address ! (and is not deleted)

By checking in the data base (in email_addr_bean_rel), I have record which are created, with "Accounts" in the bean columns... except, the bean_id column is empty AND strangely, the "deleted" is set to 1 ! So the relation has been made, but with no bean_id and has been deleted !

Here is a sample of the record i have in email_addr_bean_rel :

Id => relation_Id; email_address_id => my_email_address_id; bean_id => <empty>; bean_module => Accounts;  primary_address => 1; reply_to_address=>0; date_created=>the_date_entered; date_modified=>the_date_modified; deleted=> 1

I don't know where the problem come from and i don't know how i can fix it..

Did you ever ran into this issue ?

It is very important, do you have any clue on that ?

Thanks a lot !!!

Outcomes