opt_out cannot be set to 1 via logichook

Hi all.

I have a simple bit of code which is working fine when opting out email addresses, but not opting it. I was wondering if there was any logic forcing someone to do this manually in CRM. The if block is as folows:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if ($child_lead->occasional_updates_c == 'Yes')
{
foreach($child_lead->emailAddress->addresses as $email)
{
print "<pre>";
print_r($email);
print "</pre>";
//Setting up the bean creation in a tidy way
$module = 'EmailAddresses';
$record_id = $email['email_address_id'];
$email = BeanFactory::getBean($module, $record_id, array('disable_row_level_security' => true));
$email->opt_out = 0;
$email->save();
}
} elseif ($child_lead->occasional_updates_c == 'No')
{
foreach($child_lead->emailAddress->addresses as $email)
{
//Setting up the bean creation in a tidy way
$module = 'EmailAddresses';
$record_id = $email['email_address_id'];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

As mentioned the $email->opt_out = 1; works perfectly fine but the other does not.

Thanks!