1

Configured Email-to-case in case object, so whenever email is send to specific address my trigger will check for contact based on supplied email, if not there case should not created email should send back to the user telling "your case is not created". But in my trigger using addError() method the case not created if contact does'nt exist, but email is not getting sent.

Please anyone help to achieve this...

Trigger SpamCatch1 on Case(before Insert)
{
  List<String> emailAddresses = new List<String>();

  for(Case cs: Trigger.New)
  {

   system.debug('$$$$ Contact '+cs);
   if(cs.Origin =='Email' && cs.SuppliedEmail!='')
   {
      Contact C = null;
      for(Contact cObj: [Select id, email from contact where Email = :cs.SuppliedEmail limit 1])
      {
          c = cObj;
      }
      system.debug('$$$$ Contact 8 Origin '+cs.Origin);
      system.debug('$$$$ Contact 8 Email '+cs.SuppliedEmail);

      system.debug('$$$$ Contact 11 '+c);

      if(C == null || C.Email == null||C.Email=='')
      { 
                system.debug('$$$$ Contact 14 '+cs.SuppliedEmail);
                String userEmail = cs.SuppliedEmail; 
                String body = 'Your contact doesnt exist in our system. So please register in portal url to raise a case'; 
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
                OrgWideEmailAddress Orgid = [Select Id,displayname from OrgWideEmailAddress where displayname = 'No-reply@xxxxxxx.com' limit 1];
                mail.setOrgWideEmailAddressId(Orgid.Id); 
                mail.setUseSignature(false);
                mail.setBccSender(false);
                mail.setSaveAsActivity(false);
                mail.setSubject('Automated email: Your Case is not created');
                mail.setPlainTextBody(body);
                List<String> sendTo = new List<String>();
                sendTo.add(userEmail);
                mail.setToAddresses(sendTo);       
                Messaging.sendEmail(new Messaging.SingleEmailmessage[] {mail});  


                cs.adderror('No Contacts exist with the Email id, Hence, No Case created');
       }       
     }
  • Can you check if Email deliverability is set to "All E Mail"? – Abhijeet Aug 5 '16 at 7:11
  • If I comment the cs.adderror then I am receiving the mail. – Chithra Rengasamy Aug 5 '16 at 9:00
  • BAsically, you dont want the case to be created and the email should be sent right? – Abhijeet Aug 5 '16 at 9:25
  • Yes, If contact does'nt exist need to send email and case should not be created. – Chithra Rengasamy Aug 5 '16 at 10:08
0

I finally figured out how to solve this problem of yours.Firstly create a custom field on Case (text field of 255 chars will be good).Now use the below code in your trigger:

Trigger SpamCatch1 on Case(before Insert)
{
  List<String> emailAddresses = new List<String>();
  List<String> sendTo = new List<String>();

  for(Case cs: Trigger.New)
  {

   system.debug('$$$$ Contact '+cs);
   if(cs.Origin =='Email' && cs.SuppliedEmail!='')
   {
      Contact C = null;
      for(Contact cObj: [Select id, email from contact where Email = :cs.SuppliedEmail limit 1])
      {
          c = cObj;
      }
      system.debug('$$$$ Contact 8 Origin '+cs.Origin);
      system.debug('$$$$ Contact 8 Email '+cs.SuppliedEmail);

      system.debug('$$$$ Contact 11 '+c);

      if(C == null || C.Email == null||C.Email=='')
      { 

                sendTo.add(userEmail);
                // set the value of custom field as "Spam Case"



       }


     }

  }



                String body = 'Your contact doesnt exist in our system. So please register in portal url to raise a case'; 
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
                OrgWideEmailAddress Orgid = [Select Id,displayname from OrgWideEmailAddress where displayname = 'No-reply@xxxxxxx.com' limit 1];
                mail.setOrgWideEmailAddressId(Orgid.Id); 
                mail.setUseSignature(false);
                mail.setBccSender(false);
                mail.setSaveAsActivity(false);
                mail.setSubject('Automated email: Your Case is not created');
                mail.setPlainTextBody(body);

                mail.setToAddresses(sendTo);       
                Messaging.sendEmail(new Messaging.SingleEmailmessage[] {mail}); 

}

In the trigger, you will see that i have asked you to set the value of that custom field you will create.Now create another Apex class as below:

global class casedelete{


@invocableMethod
public static void deletecases(){


List<case> lst = [select id from case where customfield__C='Spam Case'];
delete lst;


}

Now create a process builder on case object which will run everytime a record is created and put filter criteria as customfield__c equals Spam Case and in the immediate action select Apex---> mention the name of the class

It worked for me in my dev org.It will surely work for you too :)

  • Are you receiving an email? – Chithra Rengasamy Aug 5 '16 at 14:02
  • I am not getting an email. – Chithra Rengasamy Aug 5 '16 at 14:03
  • Yes... i received it. – Abhijeet Aug 5 '16 at 14:09
  • Now checked to create a case , where my email already exist in contact object. ..As per the below code the case to be created. But it didn't case. – Chithra Rengasamy Aug 5 '16 at 14:14
  • But it didn't create case...Could you help? – Chithra Rengasamy Aug 5 '16 at 14:15
0

First, your trigger as written is not bulk safe since you have a SOQL query inside of a for loop. If you were to get more than 100 Cases at time, your trigger would fail with the dreaded 101 SOQL exception. Also, if there are additional triggers in the same execution context, then you'd have even fewer queries available before the entire transaction would fail.

Since your Cases are coming in via Web-to-Case, you cannot use the addError() method. Doing so causes all of the records coming in to fail in that transaction. Instead, the record needs to be removed from the list of records that are being processed for insertion as new Cases in Trigger.new and process them separately to send the email telling them they need to register.

Here's how you might approach that:

Trigger SpamCatch1 on Case(before Insert)
{
  List<String>emails = new List<String>();
  list<Contact>ctc = new list<Contact>();
  integer i = 0;    
  for(Case cs: Trigger.New)
  {

      if(cs.Origin =='Email' && cs.SuppliedEmail!='')
      { 
         Ctc[i] = cs.SuppliedEmail;
         emails.add(cs.SuppliedEmail);
      }

       i++;
  }

  list<Contact>ctcs = [Select id, email from contact where Email IN :emails]
  Messaging.SingleEmailMessage[] messages =
  new List<Messaging.SingleEmailMessage> {message};

  for(Contact c:ctcs)
  {

     for(i=0;i=ctc.size();i++)
     {
        if(ctc[i].Email = c.Email && c.Id != null)
        {
           ctc.[i].Id=c.Id;

        }
        else if(ctc[i].Email = c.Email && c.Id = null)                
        {

           String userEmail = ctc[i].Email; 
           String body = 'Your contact doesnt exist in our system. So please register in portal url to raise a case'; 
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
           OrgWideEmailAddress Orgid = [Select Id,displayname from OrgWideEmailAddress where displayname = 'No-reply@xxxxxxx.com' limit 1];
           mail.setOrgWideEmailAddressId(Orgid.Id); 
           mail.setUseSignature(false);
           mail.setBccSender(false);
           mail.setSaveAsActivity(false);
           mail.setSubject('Automated email: Your Case is not created');
           mail.setPlainTextBody(body);
           List<String> sendTo = new List<String>();
           sendTo.add(userEmail);
           mail.setToAddresses(sendTo);       
           messages.add(mail);  
           trigger.new.[i].Subject='Delete Me - Spam Case'; 
           trigger.new.[i].ContactId = 
           // create a contact named SPAM CONTACT and assign the contact this case along with related fields

        }

     }

  }
  Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
}        

Once you do the above just add an after insert trigger that deletes the case if the Subject is 'Delete Me - Spam Case'. You also try doing it with Process builder or possibly workflow, but a trigger would be the simplest.

  • Cases are coming in via Email-to-Case not Web-to-Case – Chithra Rengasamy Aug 8 '16 at 7:25
  • I'd expect it to work the same with Email-to-Case. As @crop said, you can also use Process Builder to delete the case too. – crmprogdev Aug 8 '16 at 15:55
0

Here is what is happening ...

  1. addError causes the transaction to fail. Sending emails is part of the transaction. Hence, as you observed, the Case doesn't get created, but as the transaction has failed, the email won't be sent. Thus ...
  2. You have to avoid addError and mark the Case with a field IsInvalid__c = true and send the email.
  3. Be sure to check your auto-response rules to omit any Cases with IsInvalid__c = true
  4. Be sure to send Cases with IsInvalid__c = true to a special InvalidQ in your assignment rules
  5. You can clean up these invalid cases in several ways - (1) The Case after insert trigger can invoke a future method to do the delete, or, (2) a Process Builder flow can call an Apex invocable method to do the delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.