function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
smitha vikramsmitha vikram 

Can some please help me in the test class for email when case is inserted for an account which has masterdetail to custom

I have written the code for sending an email whena case is inserted into an account. There is anothre custom object with Master Detail to Account called Intrested party. User can select an user email in there to set the email the case insertion should be notifed to .i am am having difficultuies in wrtiting the test class with best practices. can someone help me with same please see both in here....
public class sendEmailIntrestedPartyCaseCreateUpdate {
    
    
    public static void sendEmailtoIntrestedPartyOnCase(List<Case> listOfCases){
        system.debug('****'+listOfCases);
        Set<Id> AccountIds = new Set<Id>(); 
        Map<Id,Set<String>> mapOfInterestedP1toAccounttoUserEmail = new Map<id,Set<String>>();
        Map<Id,Set<String>> mapOfInterestedAlltoAccounttoUserEmail = new Map<id,Set<String>>();
        for(Case caseRecord: listOfCases){
            if(caseRecord.AccountId!=null){
                AccountIds.add(caseRecord.AccountId);
                system.debug('***'+AccountIds );
            }
        }
        for(Interested_Party__c ipRecords: [SELECT Id, Name, Account__c, P1_Cases__c, All_Cases__c, UserEmail__c
                                            FROM Interested_Party__c where Account__c IN: AccountIds ]){
            
                                             
            //p1 map population
            if(ipRecords.P1_Cases__c){
                if(!mapOfInterestedP1toAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                    
                    Set<String> lstUserEmails= new Set<String>();
                    lstUserEmails.add(ipRecords.UserEmail__c);
                    
                    mapOfInterestedP1toAccounttoUserEmail.put(ipRecords.Account__c,lstUserEmails);
                    system.debug('&&&&'+ mapOfInterestedP1toAccounttoUserEmail);
                    
                } else if
                    (mapOfInterestedP1toAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                        Set<String> lstUserEmails= new Set<String>();
                        lstUserEmails.add(ipRecords.UserEmail__c);
                        
                        mapOfInterestedP1toAccounttoUserEmail.get(ipRecords.Account__c).add(ipRecords.UserEmail__c);
                    }
            }
            //all cases map population
            if(ipRecords.All_Cases__c){
                if(!mapOfInterestedAlltoAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                    
                    Set<String> lstUserEmails= new Set<String>();
                    lstUserEmails.add(ipRecords.UserEmail__c);
                    
                    mapOfInterestedAlltoAccounttoUserEmail.put(ipRecords.Account__c,lstUserEmails);
                    
                } else if
                    (mapOfInterestedAlltoAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                        Set<String> lstUserEmails= new Set<String>();
                        lstUserEmails.add(ipRecords.UserEmail__c);
                        
                        mapOfInterestedAlltoAccounttoUserEmail.get(ipRecords.Account__c).add(ipRecords.UserEmail__c);
                        system.debug('&&&&'+ mapOfInterestedAlltoAccounttoUserEmail);
                    }
            }
            /*Email messgging fom here*/
            
            List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
           
            for(Case varCase: listOfCases){
                 Set<String> useremailsSet= new Set<String>();
                system.debug('case priority at this point'+ ''+ varCase.Priority);
                system.debug('mapOfInterestedP1toAccounttoUserEmail*****'  +mapOfInterestedP1toAccounttoUserEmail);
               if(mapOfInterestedAlltoAccounttoUserEmail.containsKey(varCase.AccountId)){
                  useremailsSet.addall(mapOfInterestedAlltoAccounttoUserEmail.get(varCase.AccountId));
                   system.debug('&&&&&&&((()()'+useremailsSet);
                }
               
                else if (varCase.Priority=='1-Critical'&& mapOfInterestedP1toAccounttoUserEmail.containskey(varCase.AccountId)){
                     system.debug('@@@@@#@#@#@'+ varCase.Priority + mapOfInterestedP1toAccounttoUserEmail + varCase.AccountId );
                   useremailsSet=mapOfInterestedP1toAccounttoUserEmail.get(varCase.AccountId);
                     system.debug('++++++++'+useremailsSet);
                }
                List<String> userEmailList = new List<String>();
                userEmailList.addaLL(useremailsSet);
                system.debug('453454353'+ userEmailList);
                IF(userEmailList.SIZE()>0){
                    Messaging.SingleEmailMessage sendEmailmsg = new Messaging.SingleEmailMessage();
                    sendEmailmsg.toaddresses= userEmailList;
                    
                    STring varcaseConcatenation= 'A new case has been created with Case number'+' '+ varCase.CaseNumber +' '+'and the associated Account Id is'+''+
                                                  + varCase.AccountId;
                    sendEmailmsg.SETplaintextbody(varcaseConcatenation);
                    String Subject = 'A new Case has been created for the account '+''+ varcase.Account_Name_Display__c;
                    sendEmailmsg.setSubject(Subject);
                    lstMsgs.add(sendEmailmsg);
                }
            }
            
            Messaging.sendEmail(lstMsgs);
            
        }
    }
}
 
@isTest
public class sendEmailIntrestedPartyCaseCreate_Test {
    
    @testSetup static void setup(){
        Account testAccount = new Account();
        testAccount.Name = 'Test for Send Email Case Trigger';
        insert testAccount;
        
        
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        UserRole ur = TestUtils.usrRole();
        User u = new User ();
        u.ProfileID = p.Id;
        u.Username ='Test@Testemail.com'+ System.currentTimeMillis(); 
        u.LastName ='TestLastname';
        u.Email ='smitha.vikram@compuware.com';
        u.Alias ='TestAlia';
        
        u.CommunityNickname ='TestCommunityNickname';
        //u.Employee_Number_CHRIS__c = '10';
        u.TimeZoneSidKey = 'America/New_York';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';
        
        u.UserRoleId = ur.Id;
        u.LanguageLocaleKey = 'en_US';
        u.Division = 'APM';
        insert u;
    
        
        
        
        Interested_Party__c iprec = new Interested_Party__c();
        iprec.User__c=u.id;
        //iprec.All_Cases__c = true;
        iprec.P1_Cases__c= true;
        iprec.Account__c=testAccount.id;
        insert iprec;
    }
    
    
    
    
    
    static testmethod void unittest(){
        
        Account newAccount=[SELECT Id, Name, Interested_Party__c FROM Account Limit 1 ];
        Test.startTest();
        Case newCase = new Case(Origin ='Live Chat',Priority='1-Critical',Status ='New',Type ='Data' ,Sub_Type__c ='Delivery Problem',
                                Subject ='Test set up' ,Description ='test again' ,Resolution_Detail__c ='test test again', Accountid= newAccount.id );
        
        insert newCase;
        
        Test.stopTest();
        
    }
    
    
    
    
    
    
    
}