0

This will be my first Apex trigger. I want to create a new case (by cloning another) when an email is sent to a case with Status = Closed. I've taken some code from a previous Stack Exchange thread to serve as my template. I'm clear as how to query and populate fields in the new case if the value is from the existing case, but what I'm unsure of how to do is to take the Text Body of the email in question and use its value to populate the Description field on the new case. Here's what I've got so far:

trigger CloneClosedCase 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);
        String LatestEmail = so.TextBody;     
    } 
}
Map<Id,case> caseMAP = new Map<Id,case>([SELECT [MY FIELDS] FROM Case WHERE id in:caseSet]);  
for(Case c:caseMAP.values()){
    if(c.Status=='Closed') {
        Case cloneCase = c.clone(false,true);
        cloneCase.parentid=c.id;
        cloneCase.Origin='Closed Case';
        cloneCase.Status='New';
        cloneCase.Description='Test Description';
        cloneList.add(cloneCase);
    }
}
try { 
    insert cloneList;      
} catch(DMLException e) {   
     System.debug('The following exception has occurred: ' + e.getMessage()); 
}
}

Of course, [MY FIELDS] is just a placeholder for the purposes of this thread. I threw in 'String LatestEmail = so.TextBody;' as a first try to define the variable containing the text body of the email. I'm assuming this is the wrong way to go about it :). Any suggestions on how and where to query the Text Body of the email in question and declare a variable to contain it would be greatly appreciated.

Thank you in advance!

1

You are close! The problem here is that your string LatestEmail is a single value and declared inside a for loop. This means that its scope is limited to usage inside that loop. You could make it visible to your loop through the existing cases, but that still won't solve the issue that you could have multiple cases and a single string value.

One option would be to use a map to store the email body with a key of the original case Id. You can then retrieve them in your case loop. (I also modified your check for the case type to avoid the prefix). I haven't tested this, so caution!

trigger CloneClosedCase on EmailMessage (after insert) {
Set<ID> caseSet = new Set<ID>();
List<Case>cloneList = new List<Case>();
Map<Id, String> emailBodyMap = new Map<Id, String>{};
for (EmailMessage so : Trigger.new) {
    if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
        caseSet.add(so.parentid);
        emailBodyMap.put(so.ParentId, so.TextBody);
    } 
}
Map<Id,case> caseMAP = new Map<Id,case>([SELECT [MY FIELDS] FROM Case WHERE id in:caseSet]);  
for(Case c:caseMAP.values()){
    if(c.Status=='Closed') {
        Case cloneCase = c.clone(false,true);
        cloneCase.parentid=c.id;
        cloneCase.Origin='Closed Case';
        cloneCase.Status='New';
        cloneCase.Description= emailBodyMap.get(c.Id);
        cloneList.add(cloneCase);
    }
}
try { 
    insert cloneList;      
} catch(DMLException e) {   
     System.debug('The following exception has occurred: ' + e.getMessage()); 
}
}
| improve this answer | |
  • Thanks for your response Daniel. Unfortunately, the updated trigger causes a Compile error, so I can't even test it yet: Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,String>].add(Id, String) at line 8 column 9 Again, I'm new at this. Research on the error thus far has been fruitless. Any further insight you can provide will be very helpful. Thanks again. – Jeremy S Oct 10 '14 at 21:17
  • Oops, I typed add instead of put for adding to the map. I just updated the code. It should compile now. Still haven't tested it, though. – Daniel Hoechst Oct 10 '14 at 21:21
  • Gotcha. I just reviewed a list of methods that can be used for Maps, and 'add' wasn't there, so it was about to click for me. Thanks again for the help. – Jeremy S Oct 10 '14 at 21:24

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.