+ Start a Discussion
s_k_as_k_a 

triiger to delete cases created from(email-to case) bounce emails

Hi,
I have a trigger  to delete cases created from bounce mails or undeliverable email (email- to-case). The triiger is not working and i am getting  the below error

The following errors were encountered while processing an incoming email:

INVALID_CROSS_REFERENCE_KEY : invalid cross reference id INVALID_CROSS_REFERENCE_KEY : invalid cross reference id

My triiger code----------------------------


trigger deleteOutOfOfficeEmailAndCases on EmailMessage (after insert) {
   
    if(trigger.isinsert && trigger.isAfter)
    {
         Set<Id> deleteEmailIds = new Set<Id>();
         set<Id> deleteCaseIds = new Set<Id>();
         List<EmailMessage> emailMsgdeleteList = new List<EmailMessage>();
         List<case> deleteCasesList = new List<Case>();
         Set<Id> applicableCaseRecTypes = new Set<Id>();        
         List<Attachment> emailAttachmentDeleteList = new List<Attachment>();
       
        
         for(EmailMessage em : trigger.new)
         {
              if(em.Incoming == true && em.ReplyToEmailMessageId == null)
                {
                    //Bounced Email delete
                    if(em.FromAddress != null)
                    {
                        // the email statarts with 'mailer-daemon' or postmaster are the bounce email address
      if(em.FromAddress.containsIgnoreCase('mailer-daemon') ||em.FromAddress.containsIgnoreCase('postmaster'))
                            deleteEmailIds.add(em.id);
                    }
                   
                }
         }        
         if(!deleteEmailIds.isEmpty())
         {
             for(Attachment atmt :[select Id,ParentId from Attachment where parentId in :deleteEmailIds])
             {
                 emailAttachmentDeleteList.add(atmt);
             }
             for(EmailMessage email : [select id, parentId from EmailMessage where id in :deleteEmailIds])
             {
                 emailMsgdeleteList.add(email);
                 deleteCaseIds.add(email.parentId);
             }
             if(!deleteCaseIds.isEmpty())
             {
                 for(Case c : [select id,casenumber, subject from case where id in :deleteCaseIds])
                 {
                    deleteCasesList.add(c);
                 }
             }        
             if(!emailMsgdeleteList.isEmpty())
             {
                 if(!emailAttachmentDeleteList.isEmpty())
                        delete emailAttachmentDeleteList;
                 delete emailMsgdeleteList;
                
                 if(!deleteCasesList.isEmpty())
                     delete deleteCasesList;
             }
         }        
   }                      

}

Can any body plz let me know how to do this.

Thanks

Best Answer chosen by s_k_a
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
After your first loop where you populate your 'deleteEmailIds' variable, pass that set of Ids into an @future method and do the work in there.

All Answers

Damien Phillippi033905702927186443Damien Phillippi033905702927186443
After your first loop where you populate your 'deleteEmailIds' variable, pass that set of Ids into an @future method and do the work in there.
This was selected as the best answer
s_k_as_k_a
Thank you,
It is working now after i changed the method to future method.