How to remove email addresses through an after save logic hook?

Hello, All.

I would like to scrub bogus email addresses (that are entered during testing, etc) through an after save logic hook. I am able to mark an email address as invalid using the code below. However, I would like to completely remove the email address (or update it as empty), so the user is prompted to enter this information next time the record is modified. Any ideas on how to correctly do this?


Thank you!

foreach ($bean->emailAddress->addresses as $k=>$emailaddress ) {

            if( $ema = $emailaddress['email_address'] ) {

                if(

                    stripos($ema,'@none.com') !== FALSE ||

                    stripos($ema,'@test.com') !== FALSE ||

                    stripos($ema,'@nunya.com') !== FALSE ||

                    stripos($ema,'@testing.com') !== FALSE

                ) {

                    $sea = new SugarEmailAddress();

                    $sea = $bean->emailAddress;

                    $sea->addAddress($ema, true, null, true);

                   

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

                   

                }

            }

        }

Parents Reply Children
  • Kenneth,

    Thanks very much for your reply. I was able to remove the email address by deleting the relationship. I did come across one other issue though. If I die before $bean->save() the relationship is deleted / change reflected on the record. Once $bean->save() is called the relationship is not deleted.  I'll be digging deeper, maybe there is a conflict somewhere else.

    if ($bean->load_relationship('email_addresses'))

                        {

                           

                            $relatedBeans = $bean->get_linked_beans('email_addresses', 'email_address');

                       

                            foreach ( $relatedBeans as $relatedBean ) {

                          

                            $bean->email_addresses->delete($bean->id, $relatedBean->id);

                            //sugar_die('If I die here the relationship is deleted');

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

                          

                            }  

                           

                         }