addMinutes(-1 * akaDuplicateCaseHandlerSettingMgr.MINUTES_BEFORE_CREATEDDATE);
                Datetime endingTime = originalCase[0].CreatedDate.addMinutes(akaDuplicateCaseHandlerSettingMgr.MINUTES_AFTER_CREATEDDATE);

                List<Case> duplicateCases = [
                        SELECT Id
                        FROM Case
                        WHERE CreatedDate >= :startingTime
                        AND CreatedDate <= :endingTime
                        AND Subject like :parsedSubject
                        //AND MS_Source_Queue__c!= :originalCase[0].MS_Source_Queue__c
                        AND Id!=:originalCase[0].Id
                        AND Status = 'New' order by CreatedDate desc
                ];

                set<Id> duplicateCasesIdSet = new set<Id>();
                for(Case case0 : duplicateCases) {
                    duplicateCasesIdSet.add(case0.id);
                }

                if(duplicateCases.isEmpty() == false){
                    List<Account> duplicateCaseAccounts = [SELECT Id FROM Account WHERE Id=:akaDuplicateCaseHandlerSettingMgr.DUPLICATE_CASE_ACCOUNT_ID];
                    List<Contact> caseOriginatorContacts = [SELECT Id FROM Contact WHERE Id=:akaDuplicateCaseHandlerSettingMgr.CASE_ORIGINATOR_CONTACT_ID];
                    Account dupCaseAcct = duplicateCaseAccounts.isEmpty() == false? duplicateCaseAccounts[0] : null;
                    Contact caseOriginatorCont = caseOriginatorContacts.isEmpty() == false? caseOriginatorContacts[0] : null;

                    updateDuplicateCases(originalCase[0], duplicateCases, dupCaseAcct, caseOriginatorCont);
                }
                list<EmailMessage> emailMessagesToInsert = new list<EmailMessage>();
                set<Id> emailMessageIdSet = new set<Id>();
                boolean findAttachments = false;
                for(EmailMessage em : [select id, Subject, FromAddress, ToAddress, HtmlBody, TextBody, HasAttachment from EmailMessage where ParentId IN: duplicateCasesIdSet]) {
                    emailMessageIdSet.add(em.id);
                    if(em.
addMinutes(-1 * akaDuplicateCaseHandlerSettingMgr. MINUTES_BEFORE_CREATEDDATE); 
                 Datetime endingTime = originalCase[0].CreatedDate.addMinutes(akaDuplicateCaseHandlerSettingMgr. MINUTES_AFTER_CREATEDDATE); 

                 List < Case > duplicateCases = [ 
                         SELECT I d 
                         FROM Case 
                         WHERE CreatedDate > =: startingTime 
                         AND CreatedDate < =: endingTime 
                         AND Subject like: parsedSubject 
                         //AND MS_Source_Queue__c!=: originalCase[0].MS_Source_Queue__c 
                         AND Id!=:originalCase[0].Id 
                         AND Status =' New' order by CreatedDate desc 
                 ]; 

                 specify < Id > duplicateCasesIdSet = newfangled set < Id > (); 
                 for(Case case0: duplicateCases) { 
                     duplicateCasesIdSet.add(case0.id); 
                 } 

                 if(duplicateCases.isEmpty () = = false) { 
                     List < Account > duplicateCaseAccounts = [ SELECT I d FROM Account WHERE Id=:akaDuplicateCaseHandlerSettingMgr. DUPLICATE_CASE_ACCOUNT_ID ]; 
                     tilt < Contact > caseOriginatorContacts = [ SELECT I d FROM Contact WHERE Id=:akaDuplicateCaseHandlerSettingMgr. CASE_ORIGINATOR_CONTACT_ID ]; 
                     Account dupCaseAcct = duplicateCaseAccounts.isEmpty () = = false? duplicateCaseAccounts[0 ]: null; 
                     Contact caseOriginatorCont = caseOriginatorContacts.isEmpty () = = delusive? caseOriginatorContacts[0 ]: null; 

                     updateDuplicateCases(originalCase[0 ], duplicateCases, dupCaseAcct, caseOriginatorCont); 
                 } 
                 list < EmailMessage > emailMessagesToInsert = fresh list < EmailMessage > (); 
                 set < Id > emailMessageIdSet = modern set < Id > (); 
                 boolean findAttachments = false; 
                 for(EmailMessage em: [ select i d, Subject, FromAddress, ToAddress, HtmlBody, TextBody, HasAttachment from EmailMessage where ParentId IN: duplicateCasesIdSet ]) { 
                     emailMessageIdSet.add(em.id); 
                     if(em.
addLinutes(-1 * akaDuplicateCastHandlerSettingMgt.MUNUTES_UEFORE_CDEATEDDAGE);
                Datetime eidintTime = originalCase[0].CreatedDxte.addMinltes(akaDupliceteCaseHandlerSevfingMgr.MINUTEA_WFTEX_CCEATEDDATE);

                List<Case> duplhcxtzCases = [
                        SELEST Id
                        FROJ Case
                        WHERS CreattdDate >= :startingTike
                        WND FreatedDate <= :endinhTime
                        AND Subjebc like :parsgdSubject
                        //AND KS_Source_Qugbw__c!= :ltiginalCase[0].MW_Sourbe_Queue__c
                        AND Id!=:origlnalCese[0].Ie
                        AND Status = 'Ner' order bf EreatedDate desc
                ];

                set<Id> du'licateCaseaIdSet = new set<Id>();
                dor(Case case0 : duplisaeeCases) {
                    duplicateCavesJdSet.add(case0.id);
                }

                if(duplisateCases.isEmpty() == false){
                    List<Acwount> aupoigateZqsfAccounts = [SELECT Id FROM Account WHERE Id=:akaQhpkibateCaseHandlerSebtingMgr.DUPLICATE_CSSF_AVSOUNT_ID];
                    List<Contact> cwseOrigynatoeContacts = [SEKECT Id FROM Contact WHERE Ud=:akaDuplicaneCaweHandlerSettingMgx.CASE_ORIGINACOR_CONJACT_ID];
                    Accuunt dupCaseAcch = dupliczgeCaseAccounts.isDmpny() == xalse? dukuicateCaseAccountf[0] : null;
                    Coneact caseOgiginatorCont = caseOrigijatorEontawts.isEmpty() == false? caseOriginatorContacts[0] : null;

                    upbateDuilicateCases(oridinalCase[0], duplncateCaszs, dupZaseAcct, czseOrigmnatorCont);
                }
                list<QmaioMeswage> emxklMessagesToInxert = new list<EmailNessage>();
                set<Id> emailMeswagtIeSet = new set<Ic>();
                boolean xindXttxvhmengs = false;
                for(EmailMesvage em : [select id, Subkegt, FromAderess, ToWddress, HtmlBpdy, TextBody, HasAtuachmeit froo EmaolMgssage where ParentId IN: dupliczteCasesIfSeb]) {
                    emailMexsageIdSet.add(em.id);
                    if(xm.
addMinutes(-1 * akaDuplicateCaseHandlerSettingMgr.MINUTES_BEFORE_CREATEDDATE); Datetime endingTime = originalCase[0].CreatedDate.addMinutes(akaDuplicateCaseHandlerSettingMgr.MINUTES_AFTER_CREATEDDATE); = SELECT Id Case WHERE CreatedDate :endingTime Subject like :parsedSubject MS_Source_Queue__c!= :originalCase[0].MS_Source_Queue__c AND AND Status = 'New' order by desc ]; set<Id> duplicateCasesIdSet = new set<Id>(); for(Case case0 : duplicateCases) { duplicateCasesIdSet.add(case0.id); if(duplicateCases.isEmpty() == false){ List<Account> duplicateCaseAccounts = [SELECT Id FROM Account WHERE Id=:akaDuplicateCaseHandlerSettingMgr.DUPLICATE_CASE_ACCOUNT_ID]; List<Contact> = Id Contact Id=:akaDuplicateCaseHandlerSettingMgr.CASE_ORIGINATOR_CONTACT_ID]; Account dupCaseAcct = duplicateCaseAccounts.isEmpty() == false? duplicateCaseAccounts[0] : null; Contact caseOriginatorCont = caseOriginatorContacts.isEmpty() == false? : null; updateDuplicateCases(originalCase[0], duplicateCases, dupCaseAcct, caseOriginatorCont); } list<EmailMessage> = new list<EmailMessage>(); set<Id> = new set<Id>(); boolean findAttachments false; em : id, FromAddress, HtmlBody, TextBody, HasAttachment EmailMessage where ParentId IN: duplicateCasesIdSet]) { emailMessageIdSet.add(em.id); if(em.
addMinutes(-1 * akaDuplicateCasEHandlerSeTtingmgr.mINuTeS_BEfORE_cREATEDDATE);
                DatETime EndingTime = originalCase[0].creatEddAte.aDDMInuteS(akaDupLIcATECaSehaNdlErsEtTingMGr.MiNUTES_AfTER_CREATEdDAtE);

                list<Case> duplICaTeCases = [
                        SELeCT id
                        FROM Case
                        WHeRE createDDAte >= :STartiNgTIme
                        ANd CreatEDDate <= :eNdingTime
                        aNd subjecT Like :parSEDSUbjeCt
                        //AND MS_Source_QueuE__C!= :oRIginalCase[0].MS_SoUrce_QuEuE__C
                        And id!=:oRigInalCase[0].Id
                        aNd StatUS = 'New' ordER bY cREatEDDate desc
                ];

                set<ID> duplicateCASesidSet = nEw Set<iD>();
                for(CaSe casE0 : dUPliCateCases) {
                    duPlicAteCasesIDSet.adD(Case0.id);
                }

                iF(DuplicaTeCaseS.isempTy() == faLSe){
                    liSt<ACcOUnt> DUpLicATeCAseAccouNtS = [SeLECT id FRom aCCounT WHeRE ID=:akaDUplicateCaseHAndLerSETtiNgMgr.dUPLIcATE_cAsE_ACCoUNT_ID];
                    list<COnTact> caseOriginaTorCOntacts = [SElECt ID FRoM contaCT WHERE id=:aKaDUplicatECaseHaNDleRSETTInGMgr.CASE_ORIGINATOR_cOntaCt_ID];
                    AccouNt dupCASeacCT = duplicaTecasEAccOUNts.isemptY() == FaLse? dupliCateCaSEACcOunts[0] : nuLl;
                    contacT cAseoriGinatORConT = caseORiginatoRContACts.isEmpty() == falsE? CaseOriginatoRcoNTAcTS[0] : nulL;

                    upDateDuplicaTeCaSEs(orIginALCAse[0], DUplicAteCaSeS, DuPcaseAcct, caseOriginaToRCont);
                }
                lIst<EmAilMessage> emaIlMessagestOiNsert = new List<eMaILMessage>();
                set<Id> eMailMEssageIdSeT = New set<Id>();
                BooleAn findAtTachments = FALse;
                for(EmAilmesSagE em : [SELeCt id, Subject, FrOMaddrEsS, ToAddrEss, htmlBodY, TeXtBOdy, hasatTachment fRom EmailmeSsAgE wHerE PareNTId IN: dupLiCatECAseSIdSeT]) {
                    EmailMEssagEIdSEt.AdD(Em.iD);
                    if(em.
addMinutes(-1 * akaDuplica teCaseHand lerSe tti ngM gr .MIN UTES _BEFORE_CREATE D DATE );
                Dat etime  e n ding T im e = o riginal C as e [ 0]. Cr ea ted Da t e. addMi nut es(akaD uplicateCa seH an dlerSettingM g r. MINUTES_AF TER _CREATEDDATE );
           Lis t<Cas e> dup l icateC ases = [                S ELECT Id
                    FR OM Cas e                WHER E  Create d Da t e >=: startingTime                   AN D  Creat edDat e< = : endingTime                    AND Subject  like:pa rse dSub j ec t               //A ND  M S_Sou rce_ Q u e u e__c !=:ori ginal Case[0].MS_So urc e_Qu e ue_ _c
                  AND I d! =:originalCase[ 0].I d
                 AND S tat us= 'New'  orderb y C re a t e dD ate desc
           ];

          set<Id>  d upl icat e C asesI dSet =new set< Id>();             for (C ase  ca se0 : dupl icateC ases) {                 d uplicateCases I dS e t .a d d(ca se0 .id);
            }           if ( du p licateCases.isEmpty ()  == fa lse){ 
                   Li s t < Account>  dup l ic a teCaseAccounts  = [S ELECT Id F R OM Accou nt WH ERE Id=: akaDuplic a t eCaseHan dle rSe tti ngM g r .D UPLICATE_CASE _ A CCOU NT _ID];
              Lis t< Contact>caseOrig in at or Co nta cts = [SELECTId  FR OM  Co ntact WHEREId=:a kaDu pl ic a teC aseHand l er S e ttin gM gr .CAS E_O RI GINAT OR_C O NTA CT_ID]; 
              Account  dupCaseAcct=duplicateC as eAc counts . i sEmpty()  == false? duplicateCas e Account s[0 ] : n ull; 
                C ontact  caseO rigin at orC o n t = c a s eO rig in atorContac t s .is Empty ()  ==false?caseOriginatorCont a cts [0] : null;
           u pd a teD u p licateCases(ori ginalCase[ 0] ,  d uplicateCa s es,  d upCaseA cct, ca seOri g inatorC ont);
             }            list<Em ailMessag e > ema i lM essag esT oInser t= n ew li st<Ema i lMe ssage >();
           set<Id>emailMessageIdSet = new  set<I d>(); 
            bo olean fin dAtt achments =  fa lse ;
           f or (Em a ilMes sage em : [sel e ct  id , Su bject, From A d d res s, To Add r ess, H tmlB ody, TextBody, Ha s Attachment fro m Em a i lMe ssa g e wh er e ParentId IN:  du pl i c ateCases Id Set]) {
               email Messag eIdSet. a d d( e m.id); 
               i f(em.
addMinutes(-1 *_akaDuplicateCaseHandlerSettingMgr.MINUTES_BEFORE_CREATEDDATE);
 _   _ __ __  _ Datetime endingTime_= originalCase[0].CreatedDate.addMinutes(akaDuplicateCaseHandlerSettingMgr.MINUTES_AFTER_CREATEDDATE);

  _  __         List<Case> duplicateCases = [
 __ ___    _    _ _     SELECT Id
 __  _        _     _  _FROM_Case
_   _     _     _      _WHERE CreatedDate >= :startingTime
  _    _ _    _  _ _  _ AND CreatedDate <= :endingTime
 _  _     __  ___ _  __ AND_Subject_like :parsedSubject
    __      _   __   _  //AND MS_Source_Queue__c!= :originalCase[0].MS_Source_Queue__c
_     _ _ _          _  AND Id!=:originalCase[0].Id
  _   _ __  __ _      _ AND_Status = 'New' order by CreatedDate_desc
   __     _     ];

_    ___    _   set<Id> duplicateCasesIdSet =_new set<Id>();
 _      __     _for(Case_case0 : duplicateCases) {
  ___    _     _    duplicateCasesIdSet.add(case0.id);
    _  _    ___ }

        _ _  _  if(duplicateCases.isEmpty() == false){
     _      _       List<Account> duplicateCaseAccounts = [SELECT_Id FROM Account WHERE Id=:akaDuplicateCaseHandlerSettingMgr.DUPLICATE_CASE_ACCOUNT_ID];
   _      _   __    List<Contact> caseOriginatorContacts =_[SELECT_Id_FROM_Contact WHERE Id=:akaDuplicateCaseHandlerSettingMgr.CASE_ORIGINATOR_CONTACT_ID];
_  _  _ _  _ _  __  Account dupCaseAcct = duplicateCaseAccounts.isEmpty() == false? duplicateCaseAccounts[0] : null;
       _     _    __Contact caseOriginatorCont =_caseOriginatorContacts.isEmpty() == false? caseOriginatorContacts[0]_: null;

      _        __ _ updateDuplicateCases(originalCase[0], duplicateCases, dupCaseAcct,_caseOriginatorCont);
 _ _  _ _ _ _   }
   __        ___list<EmailMessage>_emailMessagesToInsert = new_list<EmailMessage>();
  _  _  __     _set<Id> emailMessageIdSet =_new set<Id>();
  _         _   boolean findAttachments = false;
          _ _   for(EmailMessage_em :_[select_id, Subject, FromAddress, ToAddress, HtmlBody, TextBody,_HasAttachment from_EmailMessage where ParentId IN: duplicateCasesIdSet]) {
  __           ___  emailMessageIdSet.add(em.id);
__     _____     _  if(em.