How to add email address to User programatically?

Hello. I am writing a custom REST API to push users from another product of ours into CRM (on premise, not sugar cloud). The code that handles saving or editing the user is the following:

private function processUserBean($user_bean,string $user_name,string $user_hash,string $first_name,string $last_name,string $title,string $email,int $is_admin,string $status):void
{
 $user_bean->user_name=$user_name;
 $user_bean->first_name=$first_name;
 $user_bean->last_name=$last_name;
 $user_bean->title=$title;
 $user_bean->status=$status;
 $user_bean->is_admin=$is_admin;
 $user_bean->user_hash=$user_hash;
 if (!empty($email))
 {
  $user_bean->emailAddress->addAddress($email);
 }
 $user_bean->save();
}

Every forum and documentation directs me to this ->emailAddress->addAddress approach. This ALMOST works. What happens is the following: The User is created correctly in the database (record in users table), the email address is created correctly (record in email_addresses table) BUT the link between them, a record in email_addr_bean_rel table is NOT CREATED. What am I doing wrong?

  • Did you try saving the bean first, add the address to to it and save the user bean again? Why aren't you using the default api's of the crm ? This should take care of alot of things ;)

  • Namely because I have the password already hashed with no way of knowing what the original password is. The default API expects cleartext password and also does not handle the email situation, only the User situation. Let me try your suggestion with the double save (I would be weird, as both the User and the EmailAddress saves correctly, only the link/relation does not)

  • ,

    I think is correct. 

    When you use $user_bean->emailAddress->addAddress($email); you are effectively trying to create a relationship between the record in the users table and the record in the email_addresses table in the email_addr_bean_rel table. 

    To do that, both the email address and the user record have to have an id. If the users.id does not yet exist (it has yet to be saved and therefore yet to have an id) you can't relate it to the address.

    Having said all that, check this thread too as there is a known bug with the addAddress method that may be relevant to you.

    https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/5186/bean--emailaddress--addaddress-is-not-working-and-sea--addaddress-removes-all-other-addresses

    FrancescaS

  • Interesting. Actually this is a DESIRED functionality by me, not a bug. Our other product assumes 1 user = 1 unique email and no more.

  • I tried saving the user bean, calling the addAddress method and saving again, no luck

  • ,

    Did you follow all the instructions in the link  posted? I think you may be re-saving the User bean when what you need to do is explicitly save the email address link bean. I think your code needs to be something like:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ... //all the rest of your code;
    $user_bean->save();
    if (!empty($email))
    {
    $user_bean->emailAddress->addAddress($email);
    $user_bean->emailAddress->save($user_bean->id, $user_bean->module_dir);
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    If you want it to be the Primary address then simply use: addAddress($email, true) instead.

    That should properly save the linked email address to the User record.

    Thanks,

    JH.

  • Thank you, that did it. Nowhere in the docs, but this is the proper syntax. Marked your comment as answer

  • Not my answer, belongs to  - but I think maybe you didn't read all of that answer previously ;)

    Glad we have helped though, collaboration towards solutions is what this forum is about.

    Thanks,

    JH.