problem adding email address in a record with hook after or before save? #4517

Open
opened 2026-02-20 16:18:45 -05:00 by deekerman · 0 comments
Owner

Originally created by @dharmikvyas on GitHub (Apr 20, 2021).

I am trying to add email address in logic hook.
There is 3 custom fields in Members Form, 1:member_primary_email_c, 2: member_personal_email_c 3: member_enabled
So when member_enabled = true I want to sent member_primary_email_c as its primary/default email address to mail them. When member_enabled = false, I want to set member_personal_email_c as its primary/default email address.

I have company mail server. So when I create member, I create one email address from code in company mail server. Now I want to set that email address as primary so when I send mail he/she can receive it in company email address. When member is disabled, he can not access any account of company. So I want to send email to his personal email address.
Below is my code.

if($bean->member_enabled){
        $email=$member_primery_id;
    }else{
        $email=$member_personal_id;
    }
    $bean->emailAddress->addresses[0]=array(
        'email_address' => $email,
        'primary_address' => 1,
        'reply_to_address' => 0,
        'invalid_email' => 0,
        'opt_out' => 0,
        'confirm_opt_in_flag' => 0
    );
    $bean->emailAddress->saveEmail($bean->id,$bean->module_name);
    unset($bean->emailAddress->addresses[0]);

But It is not updating, as it should update.
I also tried to update this email address with Workflow but in workflow it is not updating as expected.
I tried direct save email address without any condition like

$bean->email1="testemail@testemail.com";
$bean->emailAddress->addresses[0]=array(
        'email_address' => "testemail@testemail.com",
        'primary_address' => 1,
        'reply_to_address' => 0,
        'invalid_email' => 0,
        'opt_out' => 0,
        'confirm_opt_in_flag' => 0
    );
    $bean->emailAddress->saveEmail($bean->id,$bean->module_name);
    unset($bean->emailAddress->addresses[0]);

But still no luck. Please check below image.
Screenshot 2021-04-15 at 11 15 38 PM

Originally created by @dharmikvyas on GitHub (Apr 20, 2021). I am trying to add email address in logic hook. There is 3 custom fields in Members Form, 1:member_primary_email_c, 2: member_personal_email_c 3: member_enabled So when member_enabled = true I want to sent member_primary_email_c as its primary/default email address to mail them. When member_enabled = false, I want to set member_personal_email_c as its primary/default email address. I have company mail server. So when I create member, I create one email address from code in company mail server. Now I want to set that email address as primary so when I send mail he/she can receive it in company email address. When member is disabled, he can not access any account of company. So I want to send email to his personal email address. Below is my code. ``` if($bean->member_enabled){ $email=$member_primery_id; }else{ $email=$member_personal_id; } $bean->emailAddress->addresses[0]=array( 'email_address' => $email, 'primary_address' => 1, 'reply_to_address' => 0, 'invalid_email' => 0, 'opt_out' => 0, 'confirm_opt_in_flag' => 0 ); $bean->emailAddress->saveEmail($bean->id,$bean->module_name); unset($bean->emailAddress->addresses[0]); ``` But It is not updating, as it should update. I also tried to update this email address with Workflow but in workflow it is not updating as expected. I tried direct save email address without any condition like ``` $bean->email1="testemail@testemail.com"; $bean->emailAddress->addresses[0]=array( 'email_address' => "testemail@testemail.com", 'primary_address' => 1, 'reply_to_address' => 0, 'invalid_email' => 0, 'opt_out' => 0, 'confirm_opt_in_flag' => 0 ); $bean->emailAddress->saveEmail($bean->id,$bean->module_name); unset($bean->emailAddress->addresses[0]); ``` But still no luck. Please check below image. <img width="1067" alt="Screenshot 2021-04-15 at 11 15 38 PM" src="https://user-images.githubusercontent.com/4004352/115339034-a42b6700-a1c1-11eb-8b9d-cb66e4bef146.png">
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/SuiteCRM-SuiteCRM#4517
No description provided.