I shall appreciate ur help.
Can anyone help me on this Requirement :
Requirement is :
1) Create a case with Subject line (User ) ----First Case
2) Create another case with Same Subject line [either by (Same user or different user )] .----Second Case
3)Now System should automatically identify it as a " Duplicate case and close the case " (i.e second case) .
4)Now the Case Comments of the Closed case (i.e second case) should be added to the existing case ( i.e first case) which is open.
trigger CaseTrigger on Case (before insert) {
Set<String> caseSubs = new Set<String>();
Set<Id> caseIds = new Set<Id>();
List<Case> listCases = new List<Case>();
List<CaseComment> listCaseComm = new List<CaseComment>();
List<CaseComment> listInsertCaseComm = new List<CaseComment>();
Map<String, Id> mapCasSubId = new Map<String, Id>();
Map<Id, CaseComment> mapCaseIdCasComm = new Map<Id, CaseComment>();
for(Case cas : trigger.new) {
caseSubs.add(cas.Subject);
caseIds.add(cas.Id);
}
listCases = [SELECT Id, Subject, Description FROM Case WHERE Subject IN : caseSubs AND Status = 'Closed'];
if(listCases.size() > 0) {
for(Case cas : listCases) {
mapCasSubId.put(cas.Subject, cas.Id);
caseIds.add(cas.Id);
}
listCaseComm = [SELECT Id, CommentBody, ParentId FROM CaseComment WHERE ParentId IN : caseIds];
for(CaseComment casComm : listCaseComm) {
mapCaseIdCasComm.put(casComm.ParentId, casComm);
}
for(Case cas : trigger.new) {
if(mapCasSubId.containsKey(cas.Subject)) {
cas.Status = 'Closed';
listInsertCaseComm.add(new CaseComment(ParentId = cas.Id, CommentBody = mapCaseIdCasComm.get(mapCasSubId.get(cas.Subject)).CommentBody));
}
}
if(listInsertCaseComm.size() > 0) {
insert listInsertCaseComm;
}
}
}
Thanks in advance