+ Start a Discussion
Abraham kumarAbraham kumar 

Test Class Code Coverage problem

Hi All.
I have the below trigger and test class and with this test class i can get only 69% coverage, can you please help to increase code coverage for below trigger.
Trigger:-
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D8798757BaFA' && c.Registered__c == TRUE)
            c.status__c='inactive';
            {
                validContacts.add(c);
                accIds.add(c.accountid);
            }   
        }
    }
    else
    {
for (contact c : Trigger.new) {
    if(c.RecordTypeId == '012D9876545BaFA' && c.Reg__c == TRUE){
    if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Registered__c == TRUE)
    //  if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
                }
                }
         }   
}
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
              }
}

Test Class:-
@istest
public class ContactcalloutTestClass{
@istest
 static void contactcreate(){
contact c =new contact();
c.Email = 'abc@gmail.com';
c.Salutation = 'Mr';
c.FirstName = 'Abraham';
c.LastName = 'Daniel';
c.type__c = 'Agency';
c.status__c= 'Active';

insert c;
c.firstname= 'Daniel';
c.lastname= 'Abraham';
c.Email = 'def@gmail.com';
c.Status__c = 'active';
update c;
           try
           { 
               Delete c;
              // c1.Status__c = 'Inactive';
            }
           catch(DmlException e) 
           {
             System.debug('The following exception has occurred: ' + e.getMessage());
            }
            }
}

Many Thanks for your help in advance
Abraham
Alexander TsitsuraAlexander Tsitsura
Hi Abraham,

I recomened you to avoid hardcoded id in code.
c.RecordTypeId == '012D9876545BaFA'

You can use Developer console for view uncovered lines and write additional tests for it.
User-added image

Thanks, Alex
KaranrajKaranraj
Don't not hardcode the id value in your code. Instead use the following approach to get the record type Id 
Change 'MyType' into your record type name 
Schema.SObjectType.Object__c.RecordTypeInfosByName.get('MyType').RecordTypeId

Updated Test class
@istest
public class ContactcalloutTestClass{
@istest
 static void contactcreate(){

Account acc = new Account();
acc.Name = 'Test';
//other required fields
insert acc;

contact c =new contact();
c.AccountId = acc.Id;
c.Email = 'abc@gmail.com';
c.Salutation = 'Mr';
c.FirstName = 'Abraham';
c.LastName = 'Daniel';
c.type__c = 'Agency';
c.status__c= 'Active';
c.RecordTypeId = getRecordTypeId (Contact,TestRecordTypeName); // Mention your recordtype name
insert c;

c.firstname= 'Daniel';
c.lastname= 'Abraham';
c.Email = 'def@gmail.com';
c.Status__c = 'active';
update c;
           try
           { 
               Delete c;
              // c1.Status__c = 'Inactive';
            }
           catch(DmlException e) 
           {
             System.debug('The following exception has occurred: ' + e.getMessage());
            }
            }

public static Id getRecordTypeId(String ObjectName,String recordTypeName){
 Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ; 
 Schema.SObjectType s = sObjectMap.get(ObjectName) ; 
 Schema.DescribeSObjectResult resSchema = s.getDescribe() ; 
 Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName(); 
 Id rtId = recordTypeInfo.get(recordTypeName).getRecordTypeId(); 
 return rtId; 
}

}

Check this Trailhead module to learn about apex test class - https://developer.salesforce.com/trailhead/module/apex_testing

 
Abraham kumarAbraham kumar
Hi Karanraj,

i added this line c.RecordTypeId = getRecordTypeId(Contact,commercial);  in the test class and it gives the error Compile Error: Variable does not exist: Contact at line 20 column 34

And also where do i add this line in Schema.SObjectType.Object__c.RecordTypeInfosByName.get('MyType').RecordTypeId in my test class?

Thanks
Abraham