1

I'm new to Salesforce and I want to have a trigger to automatically clone a case as a new case. Example, the case has been closed and the customer replied to email, I would like to keep that case closed and clone it as new request.

Is this something that can be done via workflows or would I need to create an email trigger or similar. Ideally, I would to reference the closed case as the parent.

Thanks in advance.

Andy

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 == true){
       caseSet.add(so.parentid);
   }
}
Map<Id,case> caseMAP = new Map<Id,case>([select id, Origin, Account.Name, Product.Name, Status from Case where id in:caseSet]);    //query whichever case fields should be included in the clone
for(Case c:caseMAP.values()){
    if(c.Status == 'Closed'){
        Case tmpCase = c.clone(false,true);
        tmpCase.parentid=c.id;
        cloneList.add(tmpCase);
    }
}
try{
    insert cloneList;
}catch(DMLException de){
    system.debug(de.getDMLMessage(0));
}
}
1

Andy, This cannot be done via workflow (it doesn't support creating a record, other than a task), so you will need a trigger. I would suggest that you create a trigger on the emailmessage object, that checks to see if the parent record of the email is a closed case, and if it is, creates a new case, with the parentid as the original cases ID. Something like this. trigger CloneCaseTr 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,accountid from Case where id in:caseSet]);  //query whichever case fields should be included in the clone
    for(Case c:caseMAP.values()){
        if(c.isClosed){
            Case tmpCase = c.clone(false,true);
            tmpCase.parentid=c.id;
            cloneList.add(tmpCase);
        }
    }
    try{
        insert cloneList;
    }catch(DMLException de){
        system.debug(de.getDMLMessage(0));
    }
}
| improve this answer | |
  • Thanks. I just setup the IDE and connected to the Sandbox and will start testing it out. I normally code in C# so this is a little new to me, but I think I got most of it. Question though, what string does startsWith('500') refer to? I would think that would be the Parent case ID would have to get with code correct? – ajl7519 Feb 28 '14 at 15:57
  • the 500 is to guarantee it is a related to a case. Please accept this as your answer if it does solve your problem so others can find and leverage the code if it is helpful. – JimRae Feb 28 '14 at 16:04
  • Thanks again. I edited my original post using your code with some changes. I only wanted incoming messages to set off the trigger, so I added that flag. I'm also not 100% sure how to Select items from lookup fields, but I'm assuming I would point it to the parent table (example we have a field in Case called Account that looks up the Name field in Accounts). Also, the isClosed field you had is that a generic field behind the scenes that you can call as it's not listed in the list of fields. I had changed it to use the picklist we have for Case Status. Not sure if that matters. – ajl7519 Feb 28 '14 at 18:06
  • Also, our Sandbox doesn't look to be setup with incoming emails back into the case, which will probably inhibit the trigger from going off. Thanks again. I basically have been tasked to get this working in a days notice without any experience with Apex triggers. – ajl7519 Feb 28 '14 at 18:07
  • you can use the Incoming attribute to tell if it is an incoming message, I will update the code to reflect this. The isClosed is an API field that doesn't show on the page layout, but is set to true when the close date is set. As far as the account name, not sure what you need that for, the clone will copy any of the fields from the original case, if you want the new case related to the same Account, then copy the AccountID field, and you will get the new name. – JimRae Feb 28 '14 at 18:38

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.