<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://sugarclub.sugarcrm.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/5402/how-to-add-email-address-to-user-programatically</link><description>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</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25751?ContentTypeID=1</link><pubDate>Mon, 31 Jan 2022 09:44:21 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:7eaabf40-0ce5-446d-afe7-8fc10052c97d</guid><dc:creator>John Hawkins</dc:creator><description>&lt;p&gt;Not my answer, belongs to&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a class="internal-link view-user-profile" href="/members/francescas"&gt;Francesca Shiekh&lt;/a&gt;&amp;nbsp;- but I think maybe you didn&amp;#39;t read all of that answer previously ;)&lt;/p&gt;
&lt;p&gt;Glad we have helped though, collaboration towards solutions is what this forum is about.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;JH.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25750?ContentTypeID=1</link><pubDate>Mon, 31 Jan 2022 09:35:19 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:714a3de2-9760-4701-8904-8a276147591f</guid><dc:creator>Nagy Zoltan</dc:creator><description>&lt;p&gt;Thank you, that did it. Nowhere in the docs, but this is the proper syntax. Marked your comment as answer&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25749?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 18:33:02 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:66d675f1-2aa9-4eee-84b4-c6db4477ec95</guid><dc:creator>John Hawkins</dc:creator><description>&lt;p&gt;&lt;a class="internal-link view-user-profile" href="/members/nagyzoltan"&gt;Nagy Zoltan&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;Did you follow all the instructions in the link&amp;nbsp;&lt;a class="internal-link view-user-profile" href="/members/francescas"&gt;Francesca Shiekh&lt;/a&gt;&amp;nbsp;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:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;... //all the rest of your code;

$user_bean-&amp;gt;save();

if (!empty($email))
{
  $user_bean-&amp;gt;emailAddress-&amp;gt;addAddress($email);
  $user_bean-&amp;gt;emailAddress-&amp;gt;save($user_bean-&amp;gt;id, $user_bean-&amp;gt;module_dir);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If you want it to be the Primary address then simply use: addAddress($email, true) instead.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;That should properly save the linked email address to the User record.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;JH.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25748?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 15:57:57 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:eaa27515-7729-4381-934c-243c80e5fc84</guid><dc:creator>Nagy Zoltan</dc:creator><description>&lt;p&gt;I tried saving the user bean, calling the addAddress method and saving again, no luck&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25747?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 15:57:32 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:38484fd5-84fd-458f-8154-eda6de3a71ea</guid><dc:creator>Nagy Zoltan</dc:creator><description>&lt;p&gt;Interesting. Actually this is a DESIRED functionality by me, not a bug. Our other product assumes 1 user = 1 unique email and no more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25746?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 15:45:11 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:c1a61849-cfd9-4278-803e-4f79ff27cda6</guid><dc:creator>Francesca Shiekh</dc:creator><description>&lt;p&gt;&lt;a href="/members/nagyzoltan"&gt;Nagy Zoltan&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;I think &lt;a href="/members/jeroensomhorst21784"&gt;Jeroen Somhorst&lt;/a&gt; is correct.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When you use&amp;nbsp;&lt;span&gt;$user_bean-&amp;gt;emailAddress-&amp;gt;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.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;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&amp;#39;t relate it to the address.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Having said all that, check this thread too as there is a known bug with the addAddress method that may be relevant to you.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="/dev-club/f/questions-answers/5186/bean--emailaddress--addaddress-is-not-working-and-sea--addaddress-removes-all-other-addresses"&gt;https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/5186/bean--emailaddress--addaddress-is-not-working-and-sea--addaddress-removes-all-other-addresses&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;FrancescaS&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25744?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 13:54:19 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:e03f1356-62a7-453f-9e7c-e77595f592f6</guid><dc:creator>Nagy Zoltan</dc:creator><description>&lt;p&gt;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)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to add email address to User programatically?</title><link>https://sugarclub.sugarcrm.com/thread/25743?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 13:50:08 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:37d94c76-5b62-46f9-882d-b2357c917443</guid><dc:creator>Jeroen Somhorst</dc:creator><description>&lt;p&gt;Did you try saving the bean first, add the address to to it and save the user bean again? Why aren&amp;#39;t you using the default api&amp;#39;s of the crm ? This should take care of alot of things ;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>