Adding email id via beans

Hi
I am trying to create a contact via API and its working fine. I need to add two email ids to a saved contact. I am doing it like below. But only 1 email id is associated properly. Other email id though saved in database, its marked as deleted and hence not shown in the front end. I see some related bug on other thread but not sure how to resolve it. Any help ?

$contact = BeanFactory::newBean('Contacts');   //Create bean  using module name 
$contact->first_name = $_POST['first_name'];
$contact->save();   //Save


if(isset($_POST['email1'])) {
    $email = new SugarEmailAddress;
    $email->addAddress($_POST['email1'], true,false,false,false,null,true);
    $email->save($contact->id, "Contacts");
}

if(isset($_POST['email2'])) {
    $email = new SugarEmailAddress;
    $email->addAddress($_POST['email2'],false,false,false,false,null,true);
    $email->save($contact->id, "Contacts");
}

I don’t know how to do this with the API. But I do know that there is no field called “email2”.

There is a relationship to the email_addresses table, and the “email1” field seems to be an auxiliary field to hold the primary email in the main table… to use the second email you need to work with the relationship.

Do you need this to be remote, through the API? Or is it local and you can do it with Beans instead? It would be much simpler.

Thanks for the reply. Yes, I am doing it via local bean (entry point and then in PHP file). email1/email2 are just html fields from my custom html contact form which submit s to create a Contact.

Ah, ok then.

Try using the email_addresses relationship.

See here for example:

https://community.sugarcrm.com/community/developer/blog/2017/08/01/using-e-mail-fields-correctly-in-sugar

(most of that applies to SuiteCRM, at least the parts about the Beans)