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:

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'];
								$email = BeanFactory::getBean($module, $record_id, array('disable_row_level_security' => true));
								$email->opt_out = 1;
								$email->save();
							}
						}

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

Thanks!

Parents
  • Does anyone have any thoughts on this?>

  • Hi there

    A quick question. Is your logic defined as an after_save or before_save?

    I ask because I had to do something similar about a month ago with both Contacts instead of Leads. I could not get the save() to work on the email, and I struggled with it for a while. I dug into the code of the Contacts bean to see how emails are saved OOTB. I noticed that Contacts extends Person, which extends SugarBean. When you call save() on a Contact, it does some things and then delegates to the save() on Person.

    One of the things that the save on Person does is update the emails on the Person subclass. It does the saving of emails after coming back from calling save() on the SugarBean. Therefore, if you define an after_save hook, you have a window where you can modify the Email addresses collection on the primary Bean before the emails are saved.

    I found out that to get it to work; I needed to do the following.

    • Make sure the logic hook is defined as an "after_save" logic hook
    • Secondly, when looping through the email address, I found out that each email_address array is passed by copy, and in my case, I needed it by reference so that my changes stick.

    Here is a snippet of my code

    function syncOptOut(SugarBean $bean, $event, $arguments)
    {
            $logger = $GLOBALS["log"];
    
            //get the email attached to field email1
            $emailToSync = trim($bean->email1);
    
            //grab temp_email_opt_out_c
            $tempOptOut = $bean->ts_temp_email_opt_out_c;
    
            foreach ($bean->emailAddress->addresses as &$emailAddress) {
                    
                //each item in the addresses array should be another array.
                //if this is not the case, then log this information, break the loop,
                //and gracefully exit this function
                if (!is_array($emailAddress)) {
                    $logger->fatal("The items in the emailAddress->addresses array for module " . 
                        $bean->getModuleName() . " are not arrays as expected. Exiting gracefully.");
                    break;
                } 
    
                //If we have an email match and the current opt_out is not the 
                //same as the passed in tempOptOut, then do the update.
                if (strtolower($emailToSync) === strtolower($emailAddress["email_address"]) && 
                    $tempOptOut !== $emailAddress["opt_out"]) {
                    
                    $emailAddress['opt_out'] = $tempOptOut;
                    break;
                }
            }
    
            unset($emailAddress);
    }

    I hope it helps.

Reply
  • Hi there

    A quick question. Is your logic defined as an after_save or before_save?

    I ask because I had to do something similar about a month ago with both Contacts instead of Leads. I could not get the save() to work on the email, and I struggled with it for a while. I dug into the code of the Contacts bean to see how emails are saved OOTB. I noticed that Contacts extends Person, which extends SugarBean. When you call save() on a Contact, it does some things and then delegates to the save() on Person.

    One of the things that the save on Person does is update the emails on the Person subclass. It does the saving of emails after coming back from calling save() on the SugarBean. Therefore, if you define an after_save hook, you have a window where you can modify the Email addresses collection on the primary Bean before the emails are saved.

    I found out that to get it to work; I needed to do the following.

    • Make sure the logic hook is defined as an "after_save" logic hook
    • Secondly, when looping through the email address, I found out that each email_address array is passed by copy, and in my case, I needed it by reference so that my changes stick.

    Here is a snippet of my code

    function syncOptOut(SugarBean $bean, $event, $arguments)
    {
            $logger = $GLOBALS["log"];
    
            //get the email attached to field email1
            $emailToSync = trim($bean->email1);
    
            //grab temp_email_opt_out_c
            $tempOptOut = $bean->ts_temp_email_opt_out_c;
    
            foreach ($bean->emailAddress->addresses as &$emailAddress) {
                    
                //each item in the addresses array should be another array.
                //if this is not the case, then log this information, break the loop,
                //and gracefully exit this function
                if (!is_array($emailAddress)) {
                    $logger->fatal("The items in the emailAddress->addresses array for module " . 
                        $bean->getModuleName() . " are not arrays as expected. Exiting gracefully.");
                    break;
                } 
    
                //If we have an email match and the current opt_out is not the 
                //same as the passed in tempOptOut, then do the update.
                if (strtolower($emailToSync) === strtolower($emailAddress["email_address"]) && 
                    $tempOptOut !== $emailAddress["opt_out"]) {
                    
                    $emailAddress['opt_out'] = $tempOptOut;
                    break;
                }
            }
    
            unset($emailAddress);
    }

    I hope it helps.

Children
No Data