Skip to main content
1 of 2
ajl7519
  • 169
  • 2
  • 4
  • 15

Help with testing a Case Email Trigger

Someone on the forum was gracious enough to help me with the below code. Basically, it checks if a case is closed and there is incoming email, it will then clone the case.

Problem is how can I replicate an incoming message in a test class to test the trigger. I can create the new case and change its status to closed with the close, but would be the best way to replicate a new incoming email in the code. Is there an incoming email flag.

Thanks in advance.

trigger AutoCloneCase on EmailMessage (after insert) {
Set<ID> caseSet = new Set<ID>();
List<Case>cloneList = new List<Case>();
for (EmailMessage so : Trigger.new)
{
if(((String)so.parentid).startsWith('500') && so.Incoming)
    {
     caseSet.add(so.parentid);

    }
}
Map<Id,case> caseMAP = new Map<Id,case>([SELECT id, isClosed, Type FROM Case WHERE id in:caseSet]); 
for(Case c:caseMAP.values())
{
    if(c.isClosed && c.Type == 'Support')
    {
        Case cloneCase = c.clone(false,true);
        cloneCase.parentid=c.id;
        cloneList.add(cloneCase);
    }
}
try {
 insert cloneList;     
} catch(DMLException e) {  
  System.debug('The following exception has occurred: ' + e.getMessage());      
}
}
ajl7519
  • 169
  • 2
  • 4
  • 15