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
devloper sfdcdevloper sfdc 

why test class is not covering code coverage for all line

Hi All , 

I have cover only 58 percent code covergage for trigger but i want to 100% . Please understand my test code and tell me what the issue .
my apex trigger is .
 
trigger Prevent_Escalation_SupportType on Case (before insert,after update) {
       List<String> LstRecTypeName=new List<String>{'American Sentinel University_Ticket','Limestone_Ticket','CSU_Ticket','Extension_Ticket','Healthcare Learning Innovations_Ticket','LinnBenton_Ticket','Lipscomb_Ticket',
        'Reinhardt University_Ticket','UMKC_Ticket','Bridgeport_Ticket','GardnerWebb_Ticket','Joliet_Ticket','Lynchburg_Ticket','LakeMichiganC_Ticket','RogersState_Ticket','SCKans_Ticket','SewardCCC_Ticket','SUShreveport_Ticket','UTSW_Ticket','Benedictine University_Ticket'};
    
Set<Id>LstRecordTypIds =new Set<Id>();
    
    List<RecordType> lstrecType=[Select id From RecordType where sobjecttype = 'case' and name in:LstRecTypeName];
    
    for(RecordType Rec:lstrecType)
    {
      LstRecordTypIds.add(Rec.id);  
    }
    
    
    for(case cs:trigger.new)
   {
       if((userinfo.getLastName().contains('BBH') || (userinfo.getLastName().contains('BlackBeltHelp'))))
           {
                   
                   if(LstRecordTypIds.contains(cs.RecordTypeId))
                       {
               
                           if(cs.Support_Type__c==null) 
                              {
        
                                  if(cs.Status=='Action - Automated Escalation' ||cs.Status=='Escalated')
                                     {
          
                                      cs.addError('Please select a Support type value before escalating the ticket.');
                                       }
                                   }
                         
                               else if(cs.Support_Type__c!=null)
                                    {
                         
                                  if(!(cs.Status=='Action - Automated Escalation' ||cs.Status=='Escalated'))
                                      {
                                      cs.addError('Please make sure  Support type value is blank unless you want to escalate the ticket');
                                    }
                         
                                    } 
                             }
                     }
             }  
    
        
}



Test Class is 
@isTest
public class Prevent_Escalation_SupportTypeTest {
      @isTest
    private static void test1(){
        Account acc = new Account(Name='LimeStone');
        insert acc;
        
        Contact con = new Contact(AccountId=acc.Id,LastName='test');
        insert con;
        Trigger_Control__c tc=new Trigger_Control__c();
        tc.Enable_CaseCreation_Trigger__c=true;
        tc.Name='test tc';
        insert tc;
        User u;
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
        System.runAs (thisUser) {
            Profile p = [SELECT Id FROM Profile WHERE Name='CH_Tier1-Partner'];
            u = new User(alias = 'tstwy', email='testmail@tst.com', 
                    emailencodingkey='UTF-8', lastname='testL', 
                    languagelocalekey='en_US', 
                    localesidkey='en_US', profileid = p.Id, ContactId = con.Id,
                    timezonesidkey='America/Los_Angeles', 
                    username='testmailxxx@tst.com');
            insert u; 
        }
        
        List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'Limestone_Ticket'];
        
        if(listRecType[0].Id!=null)
        {
        System.runAs (u) {
            
            Case cs = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id );
            insert cs;
        }    
    }   
    }
}


Regards
Shiv Patel
Raj VakatiRaj Vakati
try this code
 
@isTest
public class Prevent_Escalation_SupportTypeTest {
      @isTest
    private static void test1(){
        Account acc = new Account(Name='LimeStone');
        insert acc;
        
        Contact con = new Contact(AccountId=acc.Id,LastName='test');
        insert con;
        Trigger_Control__c tc=new Trigger_Control__c();
        tc.Enable_CaseCreation_Trigger__c=true;
        tc.Name='test tc';
        insert tc;
        User u;
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
        System.runAs (thisUser) {
            Profile p = [SELECT Id FROM Profile WHERE Name='CH_Tier1-Partner'];
            u = new User(alias = 'tstwy', email='testmail@tst.com', 
                    emailencodingkey='UTF-8', lastname='BlackBeltHelp', 
                    languagelocalekey='en_US', 
                    localesidkey='en_US', profileid = p.Id, ContactId = con.Id,
                    timezonesidkey='America/Los_Angeles', 
                    username='testmailxxx@tst.com');
            insert u; 
        }
        
		 Account accoObj = new Account();
    accoObj.Name  = 'testAcc';
    accoObj.Type = 'CM';
    accoObj.BillingCountry = 'United States';
    insert accoObj ;
	
	Contact conObj = new Contact();
    conObj.FirstName = 'test';
    conObj.LastName = 'testLastname';
    conObj.AccountId = accObj.Id;
    conObj.Email = 'abc@gmail.com';
	insert conObj ;
	
	String recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Limestone_Ticket').getRecordTypeId();
        
        System.runAs (u) {
            
	Case caseObj = new Case();
    caseObj.ContactId = conObj.Id;
    caseObj.Status = 'Open';
	caseObj.Support_Type__c=null ;
	caseObj.Status='Action - Automated Escalation' ;
    caseObj.Subject = 'TestSubject';
    caseObj.Description = 'TestDescription';
    caseObj.RecordTypeId = recId;
    insert caseObj ; 
	
	
	Case caseObj1 = new Case();
    caseObj1.ContactId = conObj.Id;
    caseObj1.Status = 'Open';
	caseObj1.Support_Type__c=null ;
	caseObj1.Status='NON - Action - Automated Escalation' ;
    caseObj1.Subject = 'TestSubject';
    caseObj1.Description = 'TestDescription';
    caseObj1.RecordTypeId = recId;
    insert caseObj1 ; 
	
	
        }    
      
    }
}

 
devloper sfdcdevloper sfdc
[image: image.png] Those Number of lines is not covering code coverage after use your code.
Raj VakatiRaj Vakati
Can you check the test class is running successfully or not .. and what is the error message