• r_wick007
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
public static void updateRelatedCases(Set<Id> caseIdSet,Map<id,id> oldDupCaseMap){
        try{
            Set<Id> caseRecordTypeIds=new Set<Id>();
            caseRecordTypeIds.add(Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Product_Inquiry').getRecordTypeId());
            caseRecordTypeIds.add(Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Order_Inquiry').getRecordTypeId());
            //added by Purujeet for merging two queries
            List<Id> finalcaseIdList = new List<Id>();
            finalcaseIdList.addAll(caseIdSet);
            finalcaseIdList.addAll(oldDupCaseMap.values());

            List<Case> casesToUpdate=new list<Case>();
            Map<id,case> oldCaseMapQuery=new Map<id,Case>();
                  
            for (Case caseVar: [Select Id,CaseNumber from Case where RecordTypeId in:caseRecordTypeIds and Id in :finalcaseIdList]){

                if (caseIdSet.contains(caseVar.Id)){
                    casesToUpdate.add(caseVar);
                }
                if(oldDupCaseMap.values().contains(caseVar.Id)){
                    oldCaseMapQuery.put(caseVar.Id, caseVar);
                }
            }
            //END
            //List<Case> casesToUpdate=[select Id from Case where RecordTypeId in:caseRecordTypeIds and Id in:caseIdSet];
            //Map<id,case> oldCaseMapQuery=new Map<id,Case>([select Id,caseNumber from Case where  Id in:oldDupCaseMap.values()]);
            //purujeet to update to make sure weonly use onwe query
                                          
                                        
            for(Case objCase:casesToUpdate){

                objCase.Status='Closed';
                objCase.Sub_Status__c='Duplicate';
                objCase.CaseClosureReason__c='Duplicate Case';
                objCase.isVerificationCompleted__c=true;
                objCase.Resolution__c='Duplicate Case for '+oldCaseMapQuery.get(oldDupCaseMap.get(objCase.Id)).caseNumber;
            }
                                        
            if(!casesToUpdate.isEmpty()){
                update casesToUpdate;
            }
        }catch(Exception e){
            System.debug('Exception-->'+e);
        }
    }

test class
@isTest
    public static void updateRelatedCases()
    {
        Test.startTest();
        Account account1 = new Account(Name = 'Custom Account');
        insert account1;
        
        // Contact with  Account
        Contact contact1 = new Contact(
            FirstName = 'test',
            LastName = 'test',
            AccountId = account1.Id,
            Email = 'test2@test2-unique-address.com');
        insert contact1;
       
        Set<Id> caseRecordTypeIds=new Set<Id>();
        Id recordTypeId1 = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Product_Inquiry').getRecordTypeId();
        Id recordTypeId2 =  Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Order_Inquiry').getRecordTypeId();
        caseRecordTypeIds.add(recordTypeId1);
        caseRecordTypeIds.add(recordTypeId2);
        
        Set<Id> caseIdSet = new Set<Id>() ;
        Case case1 = new Case(
            Subject = 'Test Subject',
            Status = 'New',Origin = 'Web',
            ContactId = contact1.Id,
            AccountId = account1.Id,
            RecordTypeId = recordTypeId1);
            caseIdSet.add(case1.id);  
       
       insert case1;
        
       // Set<Id> caseIdSet = new Set<Id>() ;
        Map<Id, Id> oldDupCaseMap = new Map<Id, Id>();
        
        EmailMessage emailMessage1= new EmailMessage();
        emailMessage1.status      = '3'; // email was sent
        emailMessage1.fromAddress = 'sender@example.com'; // from address
        emailMessage1.fromName    = 'Dan Perkins'; // from name
        emailMessage1.subject     = 'This is the Subject!'; // email subject
        emailMessage1.htmlBody    = '<html><body><b>Hello</b></body></html>'; // email body
        emailMessage1.toAddress   = 'emailnotinsalesforce@toexample.com, anotherone@toexample.com';
        insert emailMessage1;  
        
        EmailMessage emailMessage2= new EmailMessage();
        emailMessage2.status      = '3'; // email was sent
        emailMessage2.fromAddress = 'sender@example.com'; // from address
        emailMessage2.fromName    = 'Dan Perkins'; // from name
        emailMessage2.subject     = 'This is the Subject!'; // email subject
        emailMessage2.htmlBody    = '<html><body><b>Hello</b></body></html>'; // email body
        emailMessage2.toAddress   = 'emailnotinsalesforce@toexample.com, anotherone@toexample.com';
        insert emailMessage2;  
        
        List<Case> casesToUpdate=new list<Case>();
        Map<id,Case> oldCaseMapQuery=new Map<id,Case>();
        
        for(Case caseVar :[Select Id,CaseNumber from Case where RecordTypeId in:caseRecordTypeIds])
        {
            if(caseIdSet.contains(caseVar.Id)){
                casesToUpdate.add(caseVar);
            }   
            if(oldDupCaseMap.values().contains(caseVar.Id)){
                oldCaseMapQuery.put(caseVar.Id, caseVar);
            }
        }
        /**/
        for(Case caeVar:casesToUpdate){

                caeVar.Status='Closed';
                caeVar.Sub_Status__c='Duplicate';
                caeVar.CaseClosureReason__c='Duplicate Case';
                caeVar.isVerificationCompleted__c=true;
                caeVar.Resolution__c='Duplicate Case for '+oldCaseMapQuery.get(oldDupCaseMap.get(caeVar.Id)).caseNumber;
            }
                                        
            if(!casesToUpdate.isEmpty()){
                update casesToUpdate;
            }
        
        EmailMessageHandler.updateRelatedCases(caseIdSet, oldDupCaseMap);
        Test.stopTest();
    }

​​​​​​​
// Assigning Contact as MMM
public class CaseHandler {
public void populateCaseContactOnInsertFromQuickAction(list<Case> newCaseList) {
  try{
    User userObject = [SELECT id, contact__c FROM user WHERE id=: Userinfo.getUserId() limit 1];
     
    if (userObject.contact__c!='' || userObject.Contact__c!=null) {
      for (Case caseVar : newCaseList) {
        if (caseVar.CaseFromMMMQuickAction__c == true) {
          caseVar.ContactId = userObject.contact__c;
        }
      }
    }
    }catch(Exception e){System.debug('Contact Id exception '+e.getMessage());}
  }
}


Here is the test class :

    @isTest
    private static void populateCaseContactOnInsertFromQuickActionTest()
    {
        try{
            Test.startTest();
            //User user = new User();
            User userObject = [SELECT id, contact__c FROM user WHERE id=: Userinfo.getUserId() limit 1];
            
            Contact con = new Contact();
            con.firstName = 'Test006';
            con.lastName= 'RSN';
            INSERT con;
            
            List<case> newCaseList = new List<Case>();
            Case caseObj = new Case(
                Status = 'Working',
                Origin = 'Phone',
               ContactId = userObject.contact__c);
            
            newCaseList.add(caseObj);
            insert newCaseList;
            
          
            
            if (userObject.contact__c!='' || userObject.Contact__c!=null) {
                for (Case caseVar : newCaseList) {
                    if (caseVar.CaseFromMDMQuickAction__c == true) {
                        caseVar.ContactId = userObject.contact__c;
                    }
                }
            }
            
          //  caseObj = [select id, casenumber from case where id = : caseObj.id];
          //  System.debug('case number' + caseObj.casenumber);
            
            CaseHandler caller = new CaseHandler();
            caller.populateCaseContactOnInsertFromQuickAction(newCaseList);
            
            Test.stopTest(); 
        }Catch(Exception exceptionObject){
            System.debug('Exception occurred : CaseTriggerTest2-->' + exceptionObject.getMessage()+' Stack trace=' + exceptionObject.getStackTraceString()); 
        }   
        
    }


IT is not covering these lines  from class:

caseVar.ContactId = userObject.contact__c;
     
    catch(Exception e){System.debug('Contact Id exception '+e.getMessage());}

  // END

public static void updateRelatedCases(Set<Id> caseIdSet,Map<id,id> oldDupCaseMap){
        try{
            Set<Id> caseRecordTypeIds=new Set<Id>();
            caseRecordTypeIds.add(Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Product_Inquiry').getRecordTypeId());
            caseRecordTypeIds.add(Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Order_Inquiry').getRecordTypeId());
            //added by Purujeet for merging two queries
            List<Id> finalcaseIdList = new List<Id>();
            finalcaseIdList.addAll(caseIdSet);
            finalcaseIdList.addAll(oldDupCaseMap.values());

            List<Case> casesToUpdate=new list<Case>();
            Map<id,case> oldCaseMapQuery=new Map<id,Case>();
                  
            for (Case caseVar: [Select Id,CaseNumber from Case where RecordTypeId in:caseRecordTypeIds and Id in :finalcaseIdList]){

                if (caseIdSet.contains(caseVar.Id)){
                    casesToUpdate.add(caseVar);
                }
                if(oldDupCaseMap.values().contains(caseVar.Id)){
                    oldCaseMapQuery.put(caseVar.Id, caseVar);
                }
            }
            //END
            //List<Case> casesToUpdate=[select Id from Case where RecordTypeId in:caseRecordTypeIds and Id in:caseIdSet];
            //Map<id,case> oldCaseMapQuery=new Map<id,Case>([select Id,caseNumber from Case where  Id in:oldDupCaseMap.values()]);
            //purujeet to update to make sure weonly use onwe query
                                          
                                        
            for(Case objCase:casesToUpdate){

                objCase.Status='Closed';
                objCase.Sub_Status__c='Duplicate';
                objCase.CaseClosureReason__c='Duplicate Case';
                objCase.isVerificationCompleted__c=true;
                objCase.Resolution__c='Duplicate Case for '+oldCaseMapQuery.get(oldDupCaseMap.get(objCase.Id)).caseNumber;
            }
                                        
            if(!casesToUpdate.isEmpty()){
                update casesToUpdate;
            }
        }catch(Exception e){
            System.debug('Exception-->'+e);
        }
    }

test class
@isTest
    public static void updateRelatedCases()
    {
        Test.startTest();
        Account account1 = new Account(Name = 'Custom Account');
        insert account1;
        
        // Contact with  Account
        Contact contact1 = new Contact(
            FirstName = 'test',
            LastName = 'test',
            AccountId = account1.Id,
            Email = 'test2@test2-unique-address.com');
        insert contact1;
       
        Set<Id> caseRecordTypeIds=new Set<Id>();
        Id recordTypeId1 = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Product_Inquiry').getRecordTypeId();
        Id recordTypeId2 =  Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Order_Inquiry').getRecordTypeId();
        caseRecordTypeIds.add(recordTypeId1);
        caseRecordTypeIds.add(recordTypeId2);
        
        Set<Id> caseIdSet = new Set<Id>() ;
        Case case1 = new Case(
            Subject = 'Test Subject',
            Status = 'New',Origin = 'Web',
            ContactId = contact1.Id,
            AccountId = account1.Id,
            RecordTypeId = recordTypeId1);
            caseIdSet.add(case1.id);  
       
       insert case1;
        
       // Set<Id> caseIdSet = new Set<Id>() ;
        Map<Id, Id> oldDupCaseMap = new Map<Id, Id>();
        
        EmailMessage emailMessage1= new EmailMessage();
        emailMessage1.status      = '3'; // email was sent
        emailMessage1.fromAddress = 'sender@example.com'; // from address
        emailMessage1.fromName    = 'Dan Perkins'; // from name
        emailMessage1.subject     = 'This is the Subject!'; // email subject
        emailMessage1.htmlBody    = '<html><body><b>Hello</b></body></html>'; // email body
        emailMessage1.toAddress   = 'emailnotinsalesforce@toexample.com, anotherone@toexample.com';
        insert emailMessage1;  
        
        EmailMessage emailMessage2= new EmailMessage();
        emailMessage2.status      = '3'; // email was sent
        emailMessage2.fromAddress = 'sender@example.com'; // from address
        emailMessage2.fromName    = 'Dan Perkins'; // from name
        emailMessage2.subject     = 'This is the Subject!'; // email subject
        emailMessage2.htmlBody    = '<html><body><b>Hello</b></body></html>'; // email body
        emailMessage2.toAddress   = 'emailnotinsalesforce@toexample.com, anotherone@toexample.com';
        insert emailMessage2;  
        
        List<Case> casesToUpdate=new list<Case>();
        Map<id,Case> oldCaseMapQuery=new Map<id,Case>();
        
        for(Case caseVar :[Select Id,CaseNumber from Case where RecordTypeId in:caseRecordTypeIds])
        {
            if(caseIdSet.contains(caseVar.Id)){
                casesToUpdate.add(caseVar);
            }   
            if(oldDupCaseMap.values().contains(caseVar.Id)){
                oldCaseMapQuery.put(caseVar.Id, caseVar);
            }
        }
        /**/
        for(Case caeVar:casesToUpdate){

                caeVar.Status='Closed';
                caeVar.Sub_Status__c='Duplicate';
                caeVar.CaseClosureReason__c='Duplicate Case';
                caeVar.isVerificationCompleted__c=true;
                caeVar.Resolution__c='Duplicate Case for '+oldCaseMapQuery.get(oldDupCaseMap.get(caeVar.Id)).caseNumber;
            }
                                        
            if(!casesToUpdate.isEmpty()){
                update casesToUpdate;
            }
        
        EmailMessageHandler.updateRelatedCases(caseIdSet, oldDupCaseMap);
        Test.stopTest();
    }

​​​​​​​