Hook to Opt_out E-Mail in Module Contacts

Hello Dear Sugar Community!

I want to put all Email-addresses in the module contacts to "opt_out" if the checkbox "inaktiv_c" is checked. 

I use a hook, but unfortunately it doesn't work. The hook itself gets triggered, i tried it with error_log. Here is my Code:

<?php

class Email_Hook
{

    function before_save_method($bean, $event, $arguments)
    {
        if ($bean->inaktiv_c) {

            if (!$bean->load_relationship('email_addresses'))
                return;
            $relatedBeans = $bean->email_addresses->getBeans();
            foreach ($relatedBeans as $relatedBean) {
                $relatedBean->opt_out = 1;
                $relatedBean->save();
            }
        }
    }
}
?>

Can you tell me what i have done wrong?

Thank You!

Parents
  • As the EmailAddresses modules is not a full function bean module just try the following in the before_save logic_hook of the contacts module and Sugar will do the job for you:

    if ($bean->inaktiv_c) {
       foreach ($bean->emailAddress->addresses as $key=>$email_bean)
       {
          $bean->emailAddress->addresses[$key]['opt_out'] = 1;
       }
    }

    As you can see addresses[$key] is not an object, it's an array only.

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

Reply
  • As the EmailAddresses modules is not a full function bean module just try the following in the before_save logic_hook of the contacts module and Sugar will do the job for you:

    if ($bean->inaktiv_c) {
       foreach ($bean->emailAddress->addresses as $key=>$email_bean)
       {
          $bean->emailAddress->addresses[$key]['opt_out'] = 1;
       }
    }

    As you can see addresses[$key] is not an object, it's an array only.

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

Children