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);

                   

                }

            }

        }

  • I think you would have to delete the relationship, I don't have an IDE in front of me but it would look something like

    $bean->load_relationship(relationship_name);

    $bean->[relationship_name]->delete($parent_id, $child_id);

     

    you would have to look at the vardefs to figure out the relationship_name, I forget what it is at the moment.

  • 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);

                          

                            }  

                           

                         }

  • Why are you trying to save() the bean after the relationship is deleted?  The delete command updates the file system but probably not the in-memory structure of the bean, so when you save() it the relationship is recreated.  Might be by design or a defect, not sure I'd have to look at it.  But in your code there, I see no reason to even call $bean->save().

  • Thanks again, I do appreciate the help. That would be my mistake. I was inappropriately calling save() in the code there and another location.