AnsweredAssumed Answered

Email Addresses on Custom Module, add programmatically

Question asked by Francesca Shiekh on Mar 12, 2019
Latest reply on Mar 15, 2019 by Francesca Shiekh

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

Outcomes