2

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

2

This was a tricky one.

I've built a complex map, containing the Cases & related CaseComments for each Subject contained in trigger.New.

I've then taken the earliest open Case per subject & left that open, whilst closing all other Cases with that subject & moving their related CaseComments to the original Case.

I've commented the code, so have a read through & give it a try, and let me know how it goes.

    trigger CaseTrigger on Case (after insert) {

    Map<String, Map<Id, Map<Case, Map<Id, CaseComment>>>> subjectCaseCommentsMap = new Map<String, Map<Id, Map<Case, Map<Id, CaseComment>>>>();
    Map<String, Id> subjectOpenCaseId = new Map<String, Id>();
    Map<Id, Case> casesToUpdate = new Map<Id, Case>();
    List<CaseComment> commentsToInsert = new List<CaseComment>();

    // Gather the subjects
    for (Case c : trigger.New) {

        if (!subjectCaseCommentsMap.containsKey(c.Subject)) {

            subjectCaseCommentsMap.put(c.Subject, new Map<Id, Map<Case, Map<Id, CaseComment>>>());
        }
    }

    // Gather all Cases with those subjects & their related CaseComments
    for (Case c : [ SELECT
                        Id,
                        Subject,
                        Description,
                        (
                            SELECT
                                Id,
                                CommentBody,
                                ParentId
                            FROM
                                CaseComments
                        )
                    FROM
                        Case
                    WHERE
                        Subject IN :subjectCaseCommentsMap.keySet()
                    AND
                        IsClosed = FALSE
                    ORDER BY
                        CreatedDate ASC ]) {

        // Add this Case to the map
        if (!subjectCaseCommentsMap.get(c.Subject).keySet().contains(c.Id)) {

            subjectCaseCommentsMap.get(c.Subject).put(c.Id, new Map<Case, Map<Id, CaseComment>>());
        }

        subjectCaseCommentsMap.get(c.Subject).get(c.Id).put(c, new Map<Id, CaseComment>());

        // Take note of the first Case for each subject
        if (subjectOpenCaseId.get(c.Subject) == null) {

            subjectOpenCaseId.put(c.Subject, c.Id);
        }
        system.debug('Line 52: Found a Case ' + c.Subject + '. Related comments: ' + c.CaseComments);
        // Add related CaseComments to the map
        if (!c.CaseComments.isEmpty()) {

            for (CaseComment com : c.CaseComments) {

                subjectCaseCommentsMap.get(c.Subject).get(c.Id).get(c).put(com.Id, com);
            }
        }
    }

    // Now see if any of those Cases need to be closed
    for (String sub : subjectCaseCommentsMap.keySet()) {

        // Only proceed if there are duplicates of this subject
        if (subjectCaseCommentsMap.get(sub).size() > 1) {

            for (Id cId : subjectCaseCommentsMap.get(sub).keySet()) {

                for (Case c : subjectCaseCommentsMap.get(sub).get(cId).keySet()) {

                    // Is this Case the first Case for this subject?
                    if (subjectOpenCaseId.get(sub) == c.Id) {

                        // Do nothing, this is the first Case for this subject

                    } else {

                        // Close this Case, and clone the CaseComments
                        c.Status = 'Closed';

                        casesToUpdate.put(c.Id, c);
                        system.debug('Line 84: Closing Case ' + c.Subject + '. Related comments: ' + c.CaseComments);
                        for (CaseComment com : c.CaseComments) {

                            commentsToInsert.add(
                                new CaseComment(
                                    CommentBody = com.CommentBody,
                                    ParentId = subjectOpenCaseId.get(sub)
                                )
                            );
                        }
                    }
                }
            }
        }
    }
    system.debug('Line 99: casesToUpdate: ' + casesToUpdate);
    system.debug('Line 100: commentsToInsert: ' + commentsToInsert);
    if (!casesToUpdate.values().isEmpty()) {

        update casesToUpdate.values();
    }

    if (!commentsToInsert.isEmpty()) {

        insert commentsToInsert;
    }
}
  • Hi Davin,Thanks for ur reply,but i do get few errors ERRORs Founded : Error: Compile Error: unexpected token: 'Map' at line 3 column 74 Error: Compile Error: unexpected token: '{' at line 11 column 59 Error: Compile Error: unexpected token: 'Not' at line 35 column 24 Error: Compile Error: unexpected token: '=' at line 36 column 35 – nikkey Aug 26 '14 at 16:41
  • i have changed line no 36 As Status!='closed' and blocked NOTIN.THe code got saved.The cases are not getting identified as duplicates and not getting closed by adding the comments to the existing case.NOTE :We are using EMAIL TO CASE PREMIUM APP.How can we achieve it..... – nikkey Aug 26 '14 at 18:59
  • Hi @nikkey, apologies - I typed that up yesterday without testing. I spun up a dev. org. this morning and made some adjustments to the code, and it's now working as expected. I've edited my original post with the working code. – Davin Casey Aug 27 '14 at 9:21
  • Hi Davin ,I was waiting for ur reply.Thanks for ur help.I have tried the code in my DEVELOPER EDItion,it worked by identifying the DUPLICATES and closing it ,but didn’t add the casecomments to the existing case(i.e previous case) – nikkey Aug 27 '14 at 11:17
  • When I tried in my Org :Enterprise Edition :the code got saved ,I have created the case(case as :0001) .When I sent an Email from Different user with same subject line (the case does not appears).Now again I have sent an email with the SAME user with same subject line (it got created as case as:0003)but did not get closed and the casecomment did not get added to the existing case.{NOTE :WE ARE USING EMAIL TO CASE PREMIUM APP :with tat the case no :0003 got identified as duplicate by adding an prefix(Email loop protection to the subject line)}.Request you to kindly check and do the needful. – nikkey Aug 27 '14 at 11:17

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.