How to add email in new record with hook after or before save?

Hello,
I would like to add new email ID in record after_save or before_save hook.
When I create new member in suitecrm, I am making one email user to our server like
record@myserver.com using after save hook. So I need to add that EmailID to that record.

@Dharmik

Welcome to community!

I recommend to use before_save. It’s special point for modify fields of current records.

Yes, I am using that right now. But the problem is email fields are dynamic, So I can set
$bean->customfield = $anydata;
$bean->save;
in logic_hooks.php but can’t set email Id field

@Dharmik

At the first. You should not call $bean->save; for logichook!

What mean is dynamic? Give me some more information .

Or, Like I have 2 custom fields, member_primery_id and member_personal_id, so when member’s custom_field member_enabled = true than primery email of that member will be member primery_id and if member_enabled=false it will be personal email as primery ID. When I set member_enabled to true, it activates email address on my mail server,(User can access that), when I set member_enabled to false, it disables email address on mail server, SO I can send mail to his personal account if enabled=false else I will send mail to my server mail address of that member…

I need to add new email id from logichooks, Like fname.lname@myserver.com and after I get response true from my mail server, I would like to update that into my email fields. also I get some ID to update that member, Like member_id=12.
I get response 12 from my api and I need to update that in member form of suitecrm. I need that ID for future hooks…

@Dharmik
If I understand you correctly you should write something like:

if(empty($_REQUEST)){ // Is API works?
    if($bean->member_enabled){
        $bean->email1=$member_primery_id;
    }else{
        $bean->email1=$member_personal_id;
    }
}

Logically that is exactly I want. But after putting this code. email1 field is updated but it is not showing in email address field…

@Dharmik

Yes, it’s a little more complicated. It’s work. I tested.

if(empty($_REQUEST)){ // Is API works?
    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]);
}

Tried this, but still no progress.
I tried with after_save and before_save both hook. Tried with $bean-save and without $bean-save.
Also logged $bean object. $bean get updated after code execute but not updating in database, so getting same result. So tried with workflow of SuiteCRM with condition. If somefieldvalue= this, than change this to that. But not updating email there also. some blogs says email portion has some issues in SuiteCRM.

@Dharmik
Delete the condition :

Yes, I did that also. below is my code.

$personal_email = $bean->member_personal_email_c;
file_put_contents($rosarioFileLog, "\n before save".print_r($bean, true)." \n", FILE_APPEND);

$bean->emailAddress->addresses[0]=array(
    'email_address' => $personal_email,
    'primary_address' => 1,
    'reply_to_address' => 0,
    'invalid_email' => 0,
    'opt_out' => 0,
    'confirm_opt_in_flag' => 0
);
$bean->email1 = $personal_email;
$bean->emailAddress->saveEmail($bean->id,$bean->module_name);
unset($bean->emailAddress->addresses[0]);
  $bean->save;
  file_put_contents($rosarioFileLog, "\n after save".print_r($bean, true)." \n", FILE_APPEND);

I can get all object in log file, means all code is executing but not getting result in SuiteCRM

@Dharmik

There are some comments.

  1. I tested decision for after_save and before_save logichooks. It works without problems.
  2. I recommend don’t call save() for variable $bean object into logichook. You can get the endless cycle.
  3. Use standard for SuiteCRM to write log file. For example:
$GLOBALS['log']->fatal(get_class()." ". __FUNCTION__." bean:\n ".print_r($bean,true));

I removed save() code. But still I am not able to get EmailAddress Updated.
My SuiteCRM Details
Version 7.11.18

Sugar Version 6.5.25 (Build 344)

@Dharmik

I am using this version too.
There is small video for you. I think that there is mistake somewhere in your code. I can not see your code fully. I very sorry but I don’t know where.

I think there is some other problem. Not code. I am also unable to update email Field from SuiteCRM workflow.

@Dharmik

  1. The $bean->email1 = $personal_email; action doesn’t make sense;
  2. Change logichook from before_save to after_save as in my video. Maybe you SuiteCRM has another code which preventing to do it.

Sorry for describing issue with less info. I have just checked suitecrm.error.log file in nginx server.
It gives me following error.
2021/04/20 09:18:43 [error] 12282#12282: *11479 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: SweeterCalc in /var/www/html/suitecrm/modules/AOW_Actions/FormulaCalculator.php on line 108" while reading response header from upstream, client: 123.201.70.169, server: suitecrm.server.org, request: "POST /index.php HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "suitecrm.server.org", referrer: "https://suitecrm.server.org/"

It says SweeterCalc is undefined. I think I need to update FormulaCalculator.php file. :confused:

I have following line on 108th line.
$this->debugEnabled = $this->configurator->config[FormulaCalculator::CONFIGURATOR_NAME]['DebugEnabled'] == 1;

@Dharmik

I don’t have any comments for it. I never saw this error. I don’t know “nginx” well. I am using “apache” usually.