+ Start a Discussion
AddaAdda 

Problem with Code Coverage - 45%

Hi All,

I am struggling with a apex class code coverage to get the code coverage upto 90%. The problem it is using a Case topic assignement functionality and sends email to different people when a topic is assigned to a case. The problem with code coverage I harded coded all the topic and wrote test class and Salesforce doesn't allow more than 10 email to fire for test class. I need to get this dynamic code coverage so that anytime I added a new topic is the code it doesn't need to hard coded the topic in the
Apex Class:

**/
public class TopicAssignmentTriggerHandler {

    
    public void sendEmailForTestimonial(List<TopicAssignment> topicAssignmentListNew) 
    {
    
        Map<Id, Topic> testimonialTopicIdsMappedTo = new Map<Id, Topic>([select Id,Name from Topic where (Name='Testimonial Feedback' OR 
        Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR
        Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' 
        OR Name='PR' OR Name='Employment Verification')]);        
        System.debug('testimonialTopicIdsMappedTo'+testimonialTopicIdsMappedTo  );
        //Create a master list to hold the emails we'll send
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        Map<Id, Id> caseIdMappedToContactId = new Map<Id, Id>();
        Map<Id, Id> caseIdMappedWithTopicAssignmentId = new Map<Id, Id>();
        Map<Id, Contact> contactIdMappedToRecord = new Map<Id, Contact>();
        Set<ID> caseIdSet = new Set<ID>();
       
        if(testimonialTopicIdsMappedTo.size() > 0 ) 
        {
         for(TopicAssignment topicAssignment :topicAssignmentListNew)
         {
          if(testimonialTopicIdsMappedTo.Keyset().contains(topicAssignment.TopicId) && topicAssignment.EntityKeyPrefix=='500') {
            caseIdSet.add(topicAssignment.EntityId);
             if(caseIdMappedWithTopicAssignmentId.keyset() != null && !caseIdMappedWithTopicAssignmentId.keyset().contains(topicAssignment.EntityId)) 
                     {
                         caseIdMappedWithTopicAssignmentId.put(topicAssignment.EntityId,topicAssignment.TopicId);
                     }                     
                }    
            }  
        }
        
        for(case caseRecordForContact : [select contactId, Id from case where ID In:caseIdSet and contactId != null]) 
        {
            caseIdMappedToContactId.put(caseRecordForContact.Id, caseRecordForContact.ContactId); 
        }
        
        for(Contact contactRecord: [select Id, Name, Email from contact where Id In:caseIdMappedToContactId.values()]) 
        {
           contactIdMappedToRecord.put(contactRecord.Id,contactRecord); 
        }
        
        for(Case caseRecord : [select Id, CaseNumber,Description,Subject,ContactId from Case where Id IN:caseIdSet]) 
        {
           contact contactRec = new contact();
           contactRec =contactIdMappedToRecord.get(caseRecord.ContactId);
           System.debug('contactRec'+contactRec);
           Id TopicId = caseIdMappedWithTopicAssignmentId.get(caseRecord.Id);
           Topic TopicRecord = testimonialTopicIdsMappedTo.get(TopicId);
           System.debug('TopicRecord '+TopicRecord);
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
           String caselink = System.URL.getSalesforceBaseUrl().toExternalForm() +'/' +caseRecord.id;
           //Set list of people who should get the email
           List<String> sendTo = new List<String>();
           List<String> EmailList= new List<String>();
           //Dummy contact to avoid sending mails to Contact attached to Case
           Contact con = new Contact();
           
           if(TopicRecord.Name.Contains('Testimonial') || TopicRecord.Name.Contains('Testimonial Feedback') || 
           TopicRecord.Name.Contains('Author Feedback') || TopicRecord.Name.Contains('Testimonial') || 
           TopicRecord.Name.Contains('Course Feedback') || TopicRecord.Name.Contains('General Feedback') || 
           TopicRecord.Name.Contains('Course Request') || TopicRecord.Name.Contains('Feature Request')  )
            {    system.debug('###############Topic record'+TopicRecord);   
                 if(TopicRecord.Name.Equals('Testimonial'))
                 {     
                     Testimonial_Topic_Assignment_EmailId__c testimonialEmailIdRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Testimonial');
                     String e=testimonialEmailIdRecord.EmailAddress__c;
                     EmailList=SplitEmail(e);
                     for(String s:EmailList)
                     {
                        sendTo.add(s);
                     }
                     System.debug('sendTo'+sendTo);
                  }
                 else 
                    {
                        Testimonial_Topic_Assignment_EmailId__c FeedbackRequestRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Feedback/Request');
                        String e=FeedbackRequestRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                 
                
                 //Set email contents - you can use variables!
            mail.setSubject(''+ TopicRecord.Name +' Topic Added To Case Number ' + caseRecord.CaseNumber + '');
            
            String body = 'Hi ,' + '<br/><br/>';
            
            body += 'Case topic has been added to the following case.'+'<br/><br/>';
            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            
            if(contactRec != null) 
            {
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Case Contact Name : ' + (contactRec.Name==null?'':contactRec.Name) + '<br/>';  
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Contact Email Id : ' + (contactRec.Email==null?'':contactRec.Email) + '<br/>'; 
            }
            
            
          //  body += '&nbsp;&nbsp;&nbsp;&nbsp;Case CaseNumber: ' + (caseRecord.CaseNumber==null?'':caseRecord.CaseNumber) + '<br/>'; 
            body += 'Case Subject : ' + (caseRecord.Subject==null?'':caseRecord.Subject) + '<br/>';
            body += 'Case Description : ' + (caseRecord.Description==null?'':caseRecord.Description) + '</br><br/>';
            body += 'Case Link :' + '<a href="' + caselink  + '">Case Link</a>'  + '<br/></br>';
          
                               
            
            body += 'Thanks,' + '</br></br/>'; 
            body += 'Lynda.com' + '</br></br>';
            
             mail.setHtmlBody(body);
             mail.setToAddresses(sendTo);
             mails.add(mail);
            }
          
            else if(TopicRecord.Name.Contains('Community Relations') || TopicRecord.Name.Contains('Legal') 
            || TopicRecord.Name.Contains('Remove From Newsletter') || TopicRecord.Name.Contains('Piracy') 
            || TopicRecord.Name.Contains('PR') || TopicRecord.Name.Contains('Employment Verification') 
            || TopicRecord.Name.Contains('Careers') )
            {    system.debug('###############Topic record'+TopicRecord);    
                 List<Testimonial_Topic_Assignment_EmailId__c> AllEmailIdRecordList= Testimonial_Topic_Assignment_EmailId__c.getall().values();
                 
                 System.debug('AllEmailIdRecordList'+AllEmailIdRecordList);
                    if(TopicRecord.Name.Contains('Community Relations'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c communityRelationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Community Relations');
                        String e=communityRelationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Legal'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c LegalRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Legal');
                        String e=LegalRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Remove From Newsletter'))
                    {
                        System.debug('TopicRecord.Name'+TopicRecord.Name);
                        Testimonial_Topic_Assignment_EmailId__c RemovefromnewslettersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Remove From Newsletter');
                        System.debug('RemovefromnewslettersRecord'+RemovefromnewslettersRecord);
                        String e=RemovefromnewslettersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Piracy'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PiracyRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Piracy');
                        String e=PiracyRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('PR'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PRRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('PR');
                        String e=PRRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Employment Verification'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c EmploymentverificationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Employment Verification');
                        String e=EmploymentverificationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Careers'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c CareersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Careers');
                        String e=CareersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    
                
                 mail.setToAddresses(sendTo);
                
            system.debug(sendTo);  
            //Set email contents
            mail.setSubject('Case Alert from Salesforce');   
            String body = 'Hello ,' + '<br/><br/>'; 
            body += 'Customer Service received the email below, and we wanted to send it your way for review. The case is now closed, but please let us know if we may be of assistance regarding this issue.'+'<br/><br/>' ;
            body += 'Thank you, and have a great day!'+'<br/><br/>'; 
            body += 'Best Regards,'+'<br/>';
            body += 'Customer Service Team '+'<br/><br/>';

            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            body += 'Email Subject : ' + caseRecord.Subject + '<br/>';
            body += 'Email Description : ' + caseRecord.Description + '<br/>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/>';
                              
            mail.setHtmlBody(body);
            //Add your email to the master list
            mails.add(mail);  
        }
        system.debug('total Emails' + mails);
        
        }
        Messaging.sendEmail(mails);
        }
        public List<String> SplitEmail(String Emails){
        List<String> EmailIdList =Emails.split(',');
        System.debug('EmailIdList '+EmailIdList );
        return EmailIdList;
    }
    
}
 
Test Class:

  

@isTest(SeeAllData=true)
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='c-aray@lynda.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
                   
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas1;
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
/*          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service' );
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
        
  */             
        
          
          
         Test.stopTest();
    }
}

test class. Can anyone please help? I am posting apex class and test class.

 
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
HI Adda,
Test class pls see below code.check it how much coverage is getting wheather below 75% you can change modification in particular test class
@istest
public class TopicAssignmentTriggerHandlerTest{
  public static testmethod void AssignmentTriggerHandlertest(){
     Test.startTest();
List<TopicAssignment> topicassignmentlist=new List<TopicAssignment>();
TopicAssignmentTriggerHandler topic=new TopicAssignmentTriggerHandler(topicassignmentlist);
topic.sendEmailForTestimonial();
     Test.stopTest();

}
}

I Hope this will help,mark it as solution if you problem solve.

Regards,
Eswar Prasad.
AddaAdda
Hi Eswar,

Here is the error I am getting
Error: Compile Error: Constructor not defined: [TopicAssignmentTriggerHandler].<Constructor>(List<TopicAssignment>) at line 6 column 37
AddaAdda
Hi Eswar,

You didn't reply to my message. I am getting the error for your code. How can I increase the code coverage? Please help

Thanks
Adda