Email Addresses on Custom Module, add programmatically

I have a custom module to which I added Email Addresses.

The Email Addresses fields appear correctly in the record view, I can edit them and I can save email addresses with the primary, invalid and opt-out flags. 

So far so good.

When I try to process and import records via code (a one-off entry point) I can't seem to add the email addresses programmatically. Eventually I will need an API to add records so this is essential to operations.

This script executes without issue. The records are added, the email validation check passes, but the email addresses are not saved.

<?php
global $db;
$q = 'select * from str_sample_users';
$result=$db->query($q);
while($item=$db->fetchByAssoc($result)){
  $widBean = BeanFactory::newBean('wid_WolfID');
  $widBean->wolfid = $item['username'];
  $widBean->name = $item['firstname'].' '.$item['lastname']. ' - ' . $item['email'];
  $widBean->reg_date = $item['regdate'];
  $widBean->save();

  $wid = BeanFactory::retrieveBean('wid_WolfID', $widBean->id);
  if(!empty($wid) && $wid->emailAddress->isValidEmail($item['email'])){
    $GLOBALS['log']->fatal("validated " . $item['email']);
    $wid->emailAddress->addAddress($item['email'],true);
  }
  $wid->save();
}

I assume I'm missing something in the save() for the bean, but I'm not sure what.

I looked at Contacts and Leads but I can't quite see where I'm going wrong.

The emailAddress is in the bean, in this case it's a pre-existing address:

   [emailAddress] => EmailAddress Object
        (
            [disable_row_level_security] => 1
            [table_name] => email_addresses
            [module_name] => EmailAddresses
            [module_dir] => EmailAddresses
            [object_name] => EmailAddress
            [disable_custom_fields] => 1
            [smarty] =>
            [addresses] => Array
                (
                    [0] => Array
                        (
                            [email_address] => 1234this@outlook.com
                            [primary_address] => 1
                            [reply_to_address] =>
                            [invalid_email] =>
                            [opt_out] =>
                            [email_address_id] => b87f2f16-4430-11e9-8fee-001a4a160170
                        )

                )

            [hasFetched] =>
            [view] =>
            [email_address] =>
            [dontLegacySave] =>
            [opt_out] =>
            [invalid_email] => 0
            [fetchedAddresses] => Array
                (
                )

            [db] => MysqliManager Object
                (
                    [dbType] => mysql
                    [variant] => mysqli
                    [priority] => 10
<..snip..>

Any thoughts?

thank you,

FrancescaS

Parents Reply Children
  • Thank your for update on deprecated email1.

    By looking at the method "addAddress" it is possible that the incoming e-mail address already exists. Double check sugarcrm.log. Hopefully you will see the error:

    SUGAREMAILADDRESS: Existing primary address could not be overriden [ {$addr} ]

    Additionally, the method "isValidEmail" is static, so if may throw some exception somehow, and indeed the method "addAddress" validates the incoming e-mail address by default unless you decide do don't do it, and sugarcrm.log may have the following record if it is an invalid e-mail:

    SUGAREMAILADDRESS: email address is not valid [ {$addr} ]

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada