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?
$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
<?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;
}
<?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
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);
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