0

Closed Cases – When a case is closed and a new email comes from customer in the same mail chain, user wants a new case to be created and tagged with the information of the previously closed case. (case details on new case like Status as new, same Origin from parent case and case-history will be copied in the new case).

Please suggest me how do i add case history from parent case?

Code :

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 id,origin,subject,Policy__c,Type,Claim__c,Complainant__c,SR_Sub_Type__c,Remarks_for_pending_status__c,status,description,parentid FROM Case WHERE id in:caseSet]);  
for(Case c:caseMAP.values()){
    if(c.Status=='Closed') {
        system.debug('@@@@@@@@@@@@@@@'+c.Id);
        system.debug('##############'+c.subject);
        Case cloneCase = c.clone(false,true);
        cloneCase.parentid=c.id;
        cloneCase.Origin=c.Origin;
        cloneCase.subject=c.subject;
        cloneCase.Policy__c=c.Policy__c;
        cloneCase.Claim__c=c.Claim__c;
        cloneCase.Type=c.Type;
        cloneCase.SR_Sub_Type__c=c.SR_Sub_Type__c;
        cloneCase.Remarks_for_pending_status__c=c.Remarks_for_pending_status__c;
        cloneCase.Status='New';
        cloneCase.Description= emailBodyMap.get(c.Id);
        cloneList.add(cloneCase);
        system.debug('*********************' +cloneList);
    }
}
try { 
    insert cloneList;      
} catch(DMLException e) {   
     System.debug('The following exception has occurred: ' + e.getMessage()); 
}
}
2
  • Where do you plan on placing the old case history? You can sub-query for the OldValue/NewValue?
    – TSmith
    Mar 16, 2017 at 11:53
  • @b1-sfdc : were you able to do this successfully? I also want to acheive same but not case history
    – Ankuli
    Aug 24, 2018 at 14:48

2 Answers 2

0

You can obtain the Case History of your "old" Case by sub-quering the Histories table.

select Id,
 CaseNumber,
 Status,
 Subject,
  (select Field, OldValue, NewValue from Histories)
from Case
where .........

I am not sure where you wish to place this data on your "new" case.

1
  • Will it be possible to clone the case History and Email chain as well along with the case. Please provide you suggestions.
    – B1 sfdc
    Mar 17, 2017 at 5:57
0

No Need to do it via code. We can do it using Process Builder and Flow combination.

Step1: Process builder on EmailMessage Object. 1st Filter Criteria :

  • IsIncoming = true
  • EmailMessage.Parent.Status = Closed
  • Plus any criteria you want to have on Case object field by traversing to CaseId field of EmailMessage.

Immediate Action Go ahead and add a step to launch flow and pass EmailMessageId and any attribute you required.

Step 2.

  • 1st element/block 'Fast Lookup' for EmailMessage record to using EmailMessage record id we received from Process builder, Add all fields you need to you are looking for Email which you want to link New Case.
  • 2nd element 'Fast Lookup' to fetch Closed Case using ParentId of EmailMessage.
  • 3rd Element 'Record Create' to create New Case and get CaseId as output.
  • 4th element 'Assignment' to clone EmailMessage variable we got from 1st element and add parentId equals to CaseId received from 3rd element.
  • 5th and last element 'Fast Create' to create EmailMessage record using variable created in 4th step.
  • 6th element 'Fast delete' to delete EmailMessage using a variable from 1st element, so it Email is not linked with Closed Case.
  • 7th You can create flow DML handle utility class and linked through fault line.
  • enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .