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
  • Maybe the code may looks like that:

      $widBean->reg_date = $item['regdate'];

      if($wid->emailAddress->isValidEmail($item['email'])){
          
    $widBean->email1 = $item['email'];
      }
      $widBean->save();

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi Francesca Shiekh,

    We could make use of ready-made vardefs templates available inside include/SugarObjects/implements.

    We need to add this entry under 'uses' array in module vardefs. For eg:

    $dictionary['<custom_module>']['uses'] = array('email_address');

    //create email fields
    VardefManager::createVardef("<custom_module_bean_name>", "<custom_module_bean_class>", array('email_address'));

    For reference, you can check out Accounts or Contacts module vardefs in cache/modules/Accounts/Accountvardefs.php

    Let us know if this works.

    Regards.

  • Hi Francesca Shiekh,

    Sorry, I just overlooked your original problem. Usually, we use SugarEmailAddress class to save email addresses related to a particular bean record. Eg as follows:

    $sea = new SugarEmailAddress;
    $sea->addAddress('test@test.com', true); 
    $sea->save($bean->id, $bean->module_dir);

     

    Alternatively, you can also use your custom bean to save an email address. Eg as follows:

    $bean->emailAddress->addAddress('test@test.com',true);
    $bean->emailAddress->save($bean->id, $bean->module_dir);

     

    where $bean is the bean of your custom module

     

    Your script misses the save call to email address bean. Please give it a try and let us know if it works.

    Regards.

  • Thank you hats,

    The problem is that in my custom module, if I add email addresses to a record via the interface using inline edit on record view or the edit view it works just fine. The address is saved and related to my custom record via the email_addr_bean_rel table entry. No problems there

    But if I try to import a number of records via spreadsheet, though it recognizes email addresses, and shows me the correct field mapping, the addresses won't import. The import will complete, all the other fields are populated, but the email addresses are not saved to the email_addresses table, and don't exist in email_addr_bean_rel.

    All the logs are clean and there is no indication of why they were not imported.

    If I try to import via code, reading in the fields and creating the bean, and use 

    $bean->emailAddress->addAddress('test@test.com',true); as you suggested I have the same problem, no email addresses are saved.

    I just tried your other suggestion:

    $bean->emailAddress->save($bean->id, $bean->module_dir);

     

    The same thing happens. No email addresses are saved.

    I checked all the logs and there are no errors anywhere.

Reply
  • Thank you hats,

    The problem is that in my custom module, if I add email addresses to a record via the interface using inline edit on record view or the edit view it works just fine. The address is saved and related to my custom record via the email_addr_bean_rel table entry. No problems there

    But if I try to import a number of records via spreadsheet, though it recognizes email addresses, and shows me the correct field mapping, the addresses won't import. The import will complete, all the other fields are populated, but the email addresses are not saved to the email_addresses table, and don't exist in email_addr_bean_rel.

    All the logs are clean and there is no indication of why they were not imported.

    If I try to import via code, reading in the fields and creating the bean, and use 

    $bean->emailAddress->addAddress('test@test.com',true); as you suggested I have the same problem, no email addresses are saved.

    I just tried your other suggestion:

    $bean->emailAddress->save($bean->id, $bean->module_dir);

     

    The same thing happens. No email addresses are saved.

    I checked all the logs and there are no errors anywhere.

Children