Email address: copy email address from a custom text field to the proper suitecrm email.object [solved]

Our Targets have a custom text field: other_email_address_c populated with an email address (I have no idea why it ended up there, I suppose from some initial CRM import).

Now we want to get rid of the wrong custom field, but we do not want to lose data, of course.

How can we move those email addresses, to the proper email_addresses table? I mean, how do we create a proper email_address object for the Target bean? is it possible?

if not, any suggestion?

thank you so much!

@fraxx

As a variant you can use massupdate and logichook after_save.
To do this code:

$newaddress=array(
    'email_address' => $bean->other_email_address_c,
    'primary_address' => 0,
    'reply_to_address' => 0,
    'invalid_email' => 0,
    'opt_out' => 0,
    'confirm_opt_in_flag' => 0
);
$bean->emailAddress->saveEmail($bean->id,$bean->module_name,$newaddress);

oh, wow, thanks you so much!

this opens up the logichook world for us, I have to study a bit, and come back for a feedback in case others face the same issue

ok, so I did this and it works:

custom/modules/Prospects/logic_hooks.php

$hook_array['after_save'][] = Array(
                          80,
                          'LabelCopyAlternativeEmail',
                          'custom/modules/Prospects/CopyAlternativeEmail.php',
                          'MyAlternativeEmailClass',
                          'MyMethodAlternativeEmail');

custom/modules/Prospects/CopyAlternativeEmail.php

$GLOBALS['log']->fatal('OK OK OK - now Entering hook CopyAlternativeEmail');

 class MyAlternativeEmailClass
 {
     function MyMethodAlternativeEmail($bean, $event, $arguments)
     {
       if(!empty($bean->other_email_address_c)){
         //Custom Logic
         // https://community.suitecrm.com/t/how-to-add-email-in-new-record-with-hook-after-or-before-save/79450/15
         // https://stackoverflow.com/questions/36651842/remove-email-address-through-after-save-logic-hook-in-sugarcrm-6-5
         $sea = new SugarEmailAddress();
         $sea = $bean->emailAddress;
         // Add NON primary email address
         $sea->addAddress($bean->other_email_address_c, false);
         // Associate the email address with the given module and record
         $sea->saveEmail($bean->id, $bean->module_name);
         $GLOBALS['log']->fatal('Logic_hook: on module '.$bean->module_name.': now the email '.$bean->other_email_address_c.' on object '.$bean->id.' should be created');
       }
     }
 }

it works very well: it copy the email from the custom field without setting it as primary

this is the SQL query to check results before and after

    SELECT prospects.id,
      first_name,
      last_name,
      prospects_cstm.other_email_address_c,  
      email_address
 FROM prospects
      LEFT JOIN prospects_cstm
             ON prospects.id = prospects_cstm.id_c
      LEFT JOIN email_addr_bean_rel
             ON email_addr_bean_rel.bean_id=prospects.id
            AND email_addr_bean_rel.bean_module = 'Prospects'
            AND email_addr_bean_rel.deleted = 0
      LEFT JOIN email_addresses
             ON email_addresses.id = email_addr_bean_rel.email_address_id

so it works well when I save, and I can save many times with no risk of adding duplicates.

Now I have to do a Mass Update (15k records): it looks like it is pretty slow: any suggestion on how to do the mass update? can I do it with PHP cli?

thank you so much! and many thanks to p.konetskiy for suggesting us the hook way!

(by the way, this is modifying ALL bean.date_modified

can this be avoided?

speed: it took a bit more than 1 hour to execute the mass update on 12k Targets.

as for avoiding changes on “date_modified”, can I execute this logic hook an all beans, without doing a mass update?

Solved with a custom scheduled task:

custom/Extension/modules/Schedulers/Ext/ScheduledTasks/MyScheduler.php

<?php

$job_strings[] = 'MyScheduler';
function MyScheduler(){
   $GLOBALS['log']->fatal('Executing my scheduler');
   require_once "include/SugarEmailAddress/SugarEmailAddress.php";
   $bean = BeanFactory::getBean('Prospects');
   $all_records = $bean->get_full_list(
            $order_by = "",
            $where = "",
            $check_dates=false,
            $show_deleted = 0);

   foreach($all_records as $my_record){
           if(!empty(trim($my_record->other_email_address_c))){
             //Custom Logic
             // https://community.suitecrm.com/t/how-to-add-email-in-new-record-with-hook-after-or-before-save/79450/15
             // https://stackoverflow.com/questions/36651842/remove-email-address-through-after-save-logic-hook-in-sugarcrm-6-5
             $sea = new SugarEmailAddress();
             $sea = $my_record->emailAddress;
             // Add NON primary email address
             $sea->addAddress($my_record->other_email_address_c, false);
             // Associate the email address with the given module and record
             $sea->saveEmail($my_record->id, $bean->module_name);
             $GLOBALS['log']->fatal('Scheduler: on module '.$my_record->module_name.': now the email '.$my_record->other_email_address_c.' on object '.$my_record->id.' should be created');
             }
   }
   //Signify we have successfully ran
   return true;
}

custom/Extension/modules/Schedulers/Ext/Language/en_us.MyScheduler.php

<?php
//We add the mod string for our method here.
$mod_strings['LBL_MYSCHEDULER'] = 'My Scheduler that copy other_email_address_c';

fix permission + repair, and apparently everything works (and really fast): as with the logic hooks, there is no worry to add duplicate email addresses, but here the date_modified is not touched.

I think we reached our goal

any comment are super welcome, since this is our first approach with all of this

@fraxx

I wanted to recommend you this way but you found it already. I sure that it is right. :+1:t2:

hey p.konetskiy,
I’ve found a serious bug in the code I’ve implemented

I wanted to add that new email address, while I see now that this on replace all bean’s email :frowning:

what did I do wrong?

I mean: If I use the “logic_hook” way, this code ADD the new email address

             $sea = new SugarEmailAddress();
             $sea = $bean->emailAddress;
             // Add NON primary email address
             $sea->addAddress($bean->other_email_address_c, false);
             // Associate the email address with the given module and record
             $sea->saveEmail($bean->id, $bean->module_name);

while if I use the same code in the scheduled task, the email is REPLACED:

   $bean = BeanFactory::getBean('Prospects');
   $all_records = $bean->get_full_list(
            $order_by = "",
            $where = "",
            $check_dates=false,
            $show_deleted = 0);

   foreach($all_records as $my_record){
             $sea = new SugarEmailAddress();
             $sea = $my_record->emailAddress;
             // Add NON primary email address
             $sea->addAddress($my_record->other_email_address_c, false);
             // Associate the email address with the given module and record
             $sea->saveEmail($my_record->id, $bean->module_name);
            

I don’t understand why…

@fraxx

Look at the post. The method saveEmail called with new array.

thanks p.konetskiy

so, after

   foreach($all_records as $my_record){

I had to add

$bean = BeanFactory::getBean('Prospects', $my_record->id);

and then work on $bean, instead of $my_record; this way it works.

so the final scheduler code is

   $bean = BeanFactory::getBean('Prospects');
   $all_records = $bean->get_full_list(
            $order_by = "",
            $where = "",
            $check_dates=false,
            $show_deleted = 0);

   foreach($all_records as $my_record){
             $bean = BeanFactory::getBean('Prospects', $my_record->id);
             $sea = new SugarEmailAddress();
             $sea = $bean->emailAddress;
             // Add NON primary email address
             $sea->addAddress($bean->other_email_address_c, false);
             // Associate the email address with the given module and record
             $sea->saveEmail($bean->id, $bean->module_name);

So, I guess $all_records = $bean->get_full_list( return a list of (array?), and not a list of beans object? to have the bean object I had to retrieve it with $bean = BeanFactory::getBean(...

I guess I can fix all of this if I use something else to get all beans instead of get_full_list

btw: I’ve tried the
$bean->emailAddress->saveEmail($bean->id,$bean->module_name,$newaddress);
way, but it did not work for me :frowning:

I

Ok, I confirm the above code is fine (and bean.modified_date is not touched), so to summarize the solution:

  • Create a custom Scheduler task for each bean with the following code (here for Targets, called Prospects in the DB):

custom/Extension/modules/Schedulers/Ext/Language/en_us.MyScheduler.php

<?php
//We add the mod string for our method here.
$mod_strings['LBL_MYSCHEDULER'] = 'My Scheduler that copy other_email_address_c';

custom/Extension/modules/Schedulers/Ext/ScheduledTasks/MyScheduler.php

<?php

$job_strings[] = 'MyScheduler';
function MyScheduler(){
   $GLOBALS['log']->fatal('Executing my scheduler');
   require_once "include/SugarEmailAddress/SugarEmailAddress.php";
   $bean = BeanFactory::getBean('Prospects');
   $all_records = $bean->get_full_list(
            $order_by = "",
            $where = "",
            $check_dates=false,
            $show_deleted = 0);

   foreach($all_records as $my_record){
           if(!empty(trim($my_record->other_email_address_c))){
             $bean = BeanFactory::getBean('Prospects', $my_record->id);
             //Custom Logic
             // https://community.suitecrm.com/t/how-to-add-email-in-new-record-with-hook-after-or-before-save/79450/15
             // https://stackoverflow.com/questions/36651842/remove-email-address-through-after-save-logic-hook-in-sugarcrm-6-5
             $sea = new SugarEmailAddress();
             $sea = $bean->emailAddress;
             // Add NON primary email address
             $sea->addAddress($bean->other_email_address_c, false);
             // Associate the email address with the given module and record
             $sea->saveEmail($bean->id, $bean->module_name);
             //$newaddress=array(
             //   'email_address'       => $bean->other_email_address_c,
             //   'primary_address'     => 0,
             //   'reply_to_address'    => 0,
             //   'invalid_email'       => 0,
             //   'opt_out'             => 0,
             //   'confirm_opt_in_flag' => 0
             //   );
             // $bean->emailAddress->saveEmail($bean->id,$bean->module_name,$newaddress);
             $GLOBALS['log']->fatal('Scheduler: on module '.$my_record->module_name.': now the email '.$my_record->other_email_address_c.' on object '.$my_record->id.' should be created');
             }
   }
   //Signify we have successfully ran
   return true;
}
  • quick repair
  • create a scheduler
  • and execute it: one time is enough, more is no harm

thank you p.konetskiy for all your valuable tips!