AnsweredAssumed Answered

Manipulating email addresses in logic hooks

Question asked by Groupe Sugar on Jun 5, 2018
Latest reply on Jun 6, 2018 by Groupe Sugar

Hello Everyone,


I would like to set an email as opt-out under certain conditions.


I created an after_save() hook.


During this after_save(), i am using the object SugarEmailAddress and more precisely the function addAddress() that allows me to flag the email address as opted out.



Here is a quick exemple that works with jobs and not with hooks : 


$sea = new SugarEmailAddress;


$addresses = $sea->getAddressesByGUID($id, 'Contacts'); 
foreach ( $addresses as $address ) {

   if($address['primary_address'] == 1 && $address['opt_out'] == 0){
        $sea->addAddress($address['email_address'], true,false,true,true); 
        $sea->addAddress($address['email_address'], false,false,false,false); 
$sea->save($id, "Contacts");


The problem is that actually, it seems that it is impossible to manipulate this object during a hook. Each time, changes are made but when the hook is done, it saves again the email addresses exactly as it was before changes, even in an after_save hook.

Then, I have tried to use the following method in my bean : 


and it works partially...


It appears that I can add a new email address with that method.

The problem is about updating an existing email address.

If my address is defined as primary, valid and opted in, it will be impossible to change it.
If it is opted-out, I will be able to update it as opted-in
If if is non-primary, I will be able to update it as primary
If it is invalid, I will be able to valid it.
It is impossible to do the reverse thing.


One more thing, this method doen't verify if I already have a primary email address. It will flag the address as primary an keep the flag to the other addresses if it is already set.
I also try the method 'AddUpdateEmailAddress' that works with SugarEmailAddress but it seems that I can't use it in this configuration.


I am a little bit lost and I can't figure out on how to manipulate emails on logicHooks.
Did anyone already have faced this problem ?


Many Thanks,
Best regards