Search code examples
emailsuitecrm

SuiteCrm Email Notifications 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 = '[email protected]';
    $email->FromName ='SuiteCRM';

    $rev_email = '[email protected]';

    /* 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);
        }       
}

}


Solution

  • The problem in duplicating in before_save hook is very common. We want the logic hook to run just the first time. To make sure of that we can utilize a static variable called $already_ran.

    class cases_email_notification
    {
      static $already_ran = false;
    
      function send_notification($bean, $event, $arguments)
      {
            $GLOBALS['log']->info("send_notification start!"
            if(self::$already_ran == true) return;
            self::$already_ran = true;
    
            /*Get sugar email engine*/
            $email = new SugarPHPMailer();
            $email->setMailerForSystem();
            $email->From = '[email protected]';
            $email->FromName ='SuiteCRM';
            /* Set Email Subject */
            $email->Subject=$subject;          
            $email->Body = ''; 
            $email->prepForOutbound();
            /* Send email to Production Department */
            $email->addAddress('[email protected]');
    
            if(!$email->Send()){
                $GLOBALS['log']->info("Could not send notification email: ". $email->ErrorInfo);
            } else {
                $GLOBALS['log']->info("I send notification email!!!"
            }
      }       
    }