Emails Duplicating

I have created a module in suitecrm that would allow users to create or view nc case. When a nc case is created an email notification is sent to the person assigned to this variable $rev_email, however when the email is sent it is being duplicated in the person’s inbox when the admin should only be receiving it once.

function createCaseEmail(&$email,$rev_email,$subject,$bean,$xtpl){
        /* Set Email Subject */
        $email->Subject=$subject;

    /* Get NC Case Number */
    $case_number = $bean ->case_number;
    $xtpl->assign('Case_Number', $case_number );

    /* Get NC Subject */
    $xtpl->assign('Case_Subject', $bean->name);

    /* Get Case Description */
    $xtpl->assign('Case_Desc', $bean->description_c);



    /* Create email message using email template and data above */
    $xtpl->parse('main');               
    $email->Body = from_html($xtpl->text('main'));


    return $email;
}
class cases_email_notification
{
function send_notification($bean, $event, $arguments)
{

    /*Get sugar email engine*/

    $email = new SugarPHPMailer();
    $email->From = 'it@gmail.com';
    $email->FromName ='SuiteCRM';

    $rev_email = 'admin@gmail.com';

    /* Get sugar template engine */
    $xtpl = new XTemplate("XTemplate/CasesEmailTemplate.html");

        /*GEt the URL for the NC Case */
        $url =  $GLOBALS['sugar_config']['site_url'].'index.php?module=Cases&action=DetailView&record='.$bean->id; 
        $xtpl -> assign('URL', $url);


        /* Get Case ID */
        $id = $bean->getFieldValue('id');

        if(empty($bean->fetched_row['id'])){

            $email=createCaseEmail($email,$rev_email,'New Case Email Notification',$bean,$xtpl);                

            /* Send email to Production Department */
            $email->addAddress($rev_email);
        }
        //Send email
        $email->isHTML(true);
        $email->prepForOutbound();
        $email->setMailerForSystem();

        if(!$email->Send()){
            $GLOBALS['log']->info("Could not send notification email: ". $email->ErrorInfo);
        }       
}

What is that code, did you write it?

Where exactly did you put it?

And what is your SuiteCRM version?

Version 7.11.2 and it is php file, I use logic hook to call the file

I asked “exactly” where did you put it, and you answered “logic hook” :unsure:

It’s likely your logic hook is getting called twice. There are many reasons why this could be happening, it would depend on which logic hook it is. It could be called once for the record creation, and once for the relationship creation.

Try to find the difference between the calls in the hook parameters, and add an “if” to make it run only on one of them.

This is the logic hook I have

$hook_array['before_save'][] = Array(2, 'Send Notification', 'custom/Extension/modules/cases/Ext/cases_email_notification.php','cases_email_notification', 'send_notification');

Where exactly is that file defining the logic hook placed, please?

I think your logic hook routine should be in the “custom/Extension/modules/Cases/Ext/LogicHooks/cases_email_notification.php” directory, check that uppercase “Cases” and the extra dir level for “LogicHooks”.

custom\modules\Cases

Ok. did you investigate what I said above? Checking if it is getting called twice, the difference between the parameters received.

So I try what you suggested and it is still coming in twice

I know that it is being called twice but I can’t figure out why

Are you using XDEBUG and an IDE?

Just set a breakpoint inside the hook, and inspect your varaibles, so you can find some difference in the args that let’s you make sending the email conditional.

I’m using notepad++

There is an XDEBUG plugin for Notepad++, though I never tried it (the plugin, I mean. I do use Notepad++ all the time, just not for coding).

Anyway, you can skip XDEBUG and just print out variable values, or their difference (array_diff)

Something in there is what you need: a sign of what kind of logic hook call you are in. And a way to make it send email only for one of two calls.