function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chandu kumarchandu kumar 

auto responce email fired

When a particular Account Contacts try to create case using email,the case should not be created and auto email should be triggered. i wrote a trigger and its avoiding creation but emails are not sending out automatically.the code is below.

Trigger SpamCatch on Case(Before Insert)
{  
  for(Case newCase: Trigger.New)
  {   
  
  if(newCase.Origin=='Email' && newCase.Subject.contains('Auto Response'))
  {
     newCase.addError('This is auto response mail. Aborting case creation.');
  }
  
  Integer Ncommunity = 0;
  
   if(newCase.Origin =='Email' && newCase.SuppliedEmail !=null  )
   {      
      for(Contact cObj: [Select id, email, Accountid from contact where email = :newCase.SuppliedEmail and Accountid in (select id from account where name =: System.Label.Ncommunity)])
      {
          Ncommunity = 1;
      }
      
      for(Contact cObj: [Select id, email, Accountid from contact where email = :newCase.SuppliedEmail and Accountid in (select id from account where name !=: System.Label.Ncommunity)])
      {
          Ncommunity = 0;
      }
          
      if ( Ncommunity == 1 )
      {
          newCase.addError('Community contacts cannot create case. Aborting case creation.');
          
          System.debugNcommunity + '3' + newCase.SuppliedEmail);
          
          List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          List<String> sendTo = new List<String>();
          sendTo.add(newCase.SuppliedEmail );
          System.debug(newCase.SuppliedEmail);
          
          mail.setToAddresses(sendTo);
          mail.setSubject('Testing for Nex Comm');
          
          mail.setReplyTo('xxxx@xxxx.com');
         
          List<String> ccTo = new List<String>();
          ccTo.add('xxxx@xxxx.com');
          mail.setCcAddresses(ccTo);
          
          String body = 'Auto generated mail, please donot response ';
          body += 'Auto generated mail, please donot response';
          mail.setHtmlBody(body);
         // mail.setTreatTargetObjectAsRecipient(true);
          mails.add(mail);
          System.debug(mail);
          Messaging.sendEmail(mails);
          
      }   
      
   }
  
  }
}

Please help on this