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
Inanskar007Inanskar007 

Test Class coverage is 0 after deployment

Hi

I have an apex trigger on contact which will trigger an email when an email field has been changed and picklist values is either No or Null

The following is the Apex trigger code 

Trigger:
trigger Contact_OnBusinessEmailChange_Trigger on Contact (after update) {
      
        List<String> mailList = new List<String>();
  List<String> mailAddresses = new List<String>(); 
  Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'TCSTestGroup'];
  for (GroupMember gm : g.groupMembers) 
  {
   mailList.add(gm.userOrGroupId);
  }
  User[] usr = [SELECT email FROM user WHERE id IN :mailList];
  for(User u : usr) 
  {
  mailAddresses.add(u.email);
  } 
        string messagebody;
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        for (Contact c : trigger.new) { 
            Contact old = trigger.oldMap.get(c.Id); 
            if ((old.Email != c.Email) && ((c.Opt_in_to_Tracking__c== 'No') ||(c.Opt_in_to_Tracking__c== null))) {  
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setToAddresses(mailAddresses);
                email.setReplyTo('noreply@salesforce.com');
                email.setSenderDisplayName('IT Salesforce Support');
                email.setSubject('Notification: Email Address Change');
                messagebody='<html><body>The Business Email for the contact "' +c.FirstName + ' ' + c.LastName + '" has been updated from ' + old.Email + ' to ' + c.Email +' by '+c.Updated_By__c + '.<br/><br/>The Contact\'s further details below:<br/><br/>Contact\'s First Name:'+c.FirstName+'<br/>Contact\'s Last Name:'+c.LastName+'<br/>Contact\'s ID:'+c.Id+'';
                email.setHtmlBody(messagebody);
                emails.add(email);
            }
        }
        Messaging.sendEmail(emails);
    }

And the below is the Apex Test Class

@isTest
private class ContactOnBusinessEmailChangeTestClass{
private static testMethod void mailchangecheckvalid(){
//for new contact valid test
   Account grpAccnt = new Account(name='UpdateAccSSTest_grp1_11Dec_2015',account_type__c='Group',Source_System_ID__c='test5923');
        insert grpAccnt;
        
        Account accnt1 = new Account(name='UpdateAccSSTest_accnt1_11Dec_2015',account_type__c='Account',Source_System_ID__c='test594',
                parentId=grpAccnt.Id);
        insert accnt1;
        Account acc1 = new Account(Name='UpdateAccSSTest_accLoc1_11Dec_2015',Account_Type__c ='Account Location',
                Active__c = true,Source_System_ID__c='test',parentId=accnt1.Id);
        insert acc1;
        Contact con1 = new Contact(FirstName='UpdateAccSSTest_con1_11Dec_2015',LastName='Test contact1',accountid=acc1.id,
                Business_Unit__c='GCF',Email='mytestmail@genre.com');
        insert con1;
  
con1.Email='mytestmail2@genre.com';
try{update con1; }catch(DmlException e){System.Debug('Failure happened');}


System.assertEquals('mytestmail2@genre.com',con1.Email);
}

private static testMethod void mailchangecheckinvalid(){
//for new contact invalid test
   Account grpAccnt = new Account(name='UpdateAccSSTest_grp2_11Dec_2015',account_type__c='Group',Source_System_ID__c='test5927');
        insert grpAccnt;
        
        Account accnt1 = new Account(name='UpdateAccSSTest_accnt2_11Dec_2015',account_type__c='Account',Source_System_ID__c='test5947',
                parentId=grpAccnt.Id);
        insert accnt1;
        Account acc1 = new Account(Name='UpdateAccSSTest_accLoc2_11Dec_2015',Account_Type__c ='Account Location',
                Active__c = true,Source_System_ID__c='test',parentId=accnt1.Id);
        insert acc1;
        Contact con1 = new Contact(FirstName='UpdateAccSSTest_con1_17Mar_2011',LastName='Test contact2',accountid=acc1.id,
                Business_Unit__c='GCF',Email='mytestmail3@genre.com');
        insert con1;

       
con1.Email='mytestmail4@genre.com';

try{update con1; }catch(DmlException e){System.Debug('Failure happened');}

System.assertnotEquals('mytestmail3@genre.com',con1.Email);
}

}

Could you please tell me how to increase the code coverage to deploy this trigger
Mike ArthurMike Arthur
Hi,

There seems to be a bug where the dev console shows 0% code coverage.  If you view from the UI it shows the actual coverage.

Go to Setup - Develop - Apex Test Execution and that will show the actual coverage.

Hope that works for you.

Mike.