• Krutarth Soni
  • NEWBIE
  • 10 Points
  • Member since 2017
  • Salesforce Developer
  • Utechsol

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 9
    Replies
Please help me to cover Schedule Class in Test Class. Help me to achieve 100% code coverage.

Schedule Class:

global class ScheduleContactDelete implements Schedulable {
    
  
    global void execute (SchedulableContext ctx)
    {
        
        // Delete Contact
        List<Contact> con = [SELECT LastName FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                       WHERE Retention_Expiration_Date__c  = :Date.Today() AND Communication_Rule_Active__c = TRUE)];
        delete con;
        system.debug('Deleted Contacts :'+ con);
        
        // Delete Contact Mail Send
        
        List<Contact> con1 = [SELECT Id, Email  FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                          WHERE Retention_Expiration_Date__c  = :Date.Today())]; 
        
     //   List<Id> lstid = new List<Id>();
        for(Contact c : con1)
        {
         //   lstid.add(c.id);
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c.Email}); 
            mail.setSubject('Consent Data Expired' ); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Dear '+c.FirstName+', <br>Your Consent Data has been deleted permanently from our System.<br>Reason: Consent for Communication has been expired. <br><br>Thank you! <br>Confident Governance Team'); 
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
            
            
        }
        
        // Consent Mail
        List<Contact> con2 = [SELECT LastName FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                        WHERE Retention_Expiration_Date__c  = :Date.Today() AND Communication_Rule_Active__c = FALSE)];
      //  List<Id> Con2ID = new List<Id>();
        for(Contact c1 : con2)
        {
       //     Con2ID.add(c1.id);
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c1.Email}); 
            mail.setSubject('Keep Consent Details' ); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Dear '+c1.FirstName+', <br><br>Communication has been stopped but we are keeping your consent details because your details are connected with our product.<br>Reason: For Legal purpose. <br>Notice: Please Contact us for future communication. <br><br>Thank you! <br>Confident Governance Team'); 
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
            
            
        }
    }
    
    
}

Test Class:
 
@isTest
private class scheduleContactDeleteTest {
    
    public static string cronExp = '0 0 0 28 2 ? 2022';
    
    static testmethod void deleteExecute(){
        
        ScheduleContactDelete ss =new ScheduleContactDelete();
        
        test.startTest();
        String jobId = System.schedule('Student Information', cronExp , new ScheduleContactDelete());        
       
        contact c= new contact();
        c.Email='test@test.com';
        c.LastName='test';
        insert c;
        
        test.stopTest();
    }
}

Thanks,
Krutarth Soni.
 
Please Help me out to cover Trigger to achieve 100 % Code Coverage.

Trigger:
 
trigger FireEmailWithEmailTemplate on Email_Template__c (after insert) {
    
    set <id> RuleID = new set<Id> ();
    for (Email_Template__c e : trigger.new)
    {
        RuleID.add(e.GDPR_Rule__c);
    }
    
    List<Contact> con = [SELECT FirstName, LastName, Email FROM Contact WHERE Id IN (SELECT Contact__c FROM Assigned_Communication_Rule__c WHERE OptinCommunicationMethod__c IN : RuleID )];
    
	for (contact c : con)
    {
        for (Email_Template__c em : trigger.new)
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c.Email}); 
            mail.setSubject(em.Subject__c); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Hello'+ c.FirstName+' ' + c.LastName+', <br><br> '+em.Email_Body__c+ ' <br> Regards, <br><br> Admin Team'); 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
        }
    }
}

Test Class
 
@isTest
public class Test_FireEmailWithEmailTemplate {
    
    static testMethod void EmailTemplate1()
    {
      
        Contact c = new Contact ();
        c.LastName = 'test';
        c.FirstName = 'test';
        c.Email = 'test@test.com';
        insert c;
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
        mail.setToAddresses(new String[] {c.Email}); 
        mail.setSubject(em.Subject__c); 
        mail.setUseSignature(false);
        mail.setHtmlBody('Hello'+ c.FirstName+' ' + c.LastName);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
        
    }
    
    
}

 
Hello,
Help me to get 100% code coverage of this code,
 
public class Overview_Controller {
    
    public string baseurl = URL.getSalesforceBaseUrl().toExternalForm();
    public string create = '/one/one.app#/alohaRedirect/'; 
    public string Framework = '/one/one.app#/sObject/CGR__CG_Framework__c/list';

    
    public PageReference CreateFramework(){
        if(UserInfo.getUiTheme()== 'Theme4d')
        {
            string FrameworkPrefix = CGR__CG_Framework__c.sObjectType.getDescribe().getKeyPrefix();
            PageReference  p = new PageReference (baseurl+create+FrameworkPrefix+'/e' );
            p.setRedirect(true);
            return p;
        }
		
		else
        {
            string FPre = CGR__CG_Framework__c.sObjectType.getDescribe().getKeyPrefix();
            PageReference p1 = new PageReference ('/'+FPre+'/e' );
            p1.setRedirect(true);
            return p1;
        }
	}
}

I tried this test class, but it only covers else part. it does not cover if (condition) block. 
 
@isTest
public class Overview_Controller_Test {
    
    private static testMethod void testOverview()
    {
        Overview_Controller oc = new Overview_Controller ();
        
        oc.CreateFramework();
    	
	}
}

Help me to achieve 100% code coverage.

--
Thank-You!
Krutarth Soni
 
Please help me to cover Schedule Class in Test Class. Help me to achieve 100% code coverage.

Schedule Class:

global class ScheduleContactDelete implements Schedulable {
    
  
    global void execute (SchedulableContext ctx)
    {
        
        // Delete Contact
        List<Contact> con = [SELECT LastName FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                       WHERE Retention_Expiration_Date__c  = :Date.Today() AND Communication_Rule_Active__c = TRUE)];
        delete con;
        system.debug('Deleted Contacts :'+ con);
        
        // Delete Contact Mail Send
        
        List<Contact> con1 = [SELECT Id, Email  FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                          WHERE Retention_Expiration_Date__c  = :Date.Today())]; 
        
     //   List<Id> lstid = new List<Id>();
        for(Contact c : con1)
        {
         //   lstid.add(c.id);
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c.Email}); 
            mail.setSubject('Consent Data Expired' ); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Dear '+c.FirstName+', <br>Your Consent Data has been deleted permanently from our System.<br>Reason: Consent for Communication has been expired. <br><br>Thank you! <br>Confident Governance Team'); 
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
            
            
        }
        
        // Consent Mail
        List<Contact> con2 = [SELECT LastName FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                        WHERE Retention_Expiration_Date__c  = :Date.Today() AND Communication_Rule_Active__c = FALSE)];
      //  List<Id> Con2ID = new List<Id>();
        for(Contact c1 : con2)
        {
       //     Con2ID.add(c1.id);
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c1.Email}); 
            mail.setSubject('Keep Consent Details' ); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Dear '+c1.FirstName+', <br><br>Communication has been stopped but we are keeping your consent details because your details are connected with our product.<br>Reason: For Legal purpose. <br>Notice: Please Contact us for future communication. <br><br>Thank you! <br>Confident Governance Team'); 
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
            
            
        }
    }
    
    
}

Test Class:
 
@isTest
private class scheduleContactDeleteTest {
    
    public static string cronExp = '0 0 0 28 2 ? 2022';
    
    static testmethod void deleteExecute(){
        
        ScheduleContactDelete ss =new ScheduleContactDelete();
        
        test.startTest();
        String jobId = System.schedule('Student Information', cronExp , new ScheduleContactDelete());        
       
        contact c= new contact();
        c.Email='test@test.com';
        c.LastName='test';
        insert c;
        
        test.stopTest();
    }
}

Thanks,
Krutarth Soni.
 
Please Help me out to cover Trigger to achieve 100 % Code Coverage.

Trigger:
 
trigger FireEmailWithEmailTemplate on Email_Template__c (after insert) {
    
    set <id> RuleID = new set<Id> ();
    for (Email_Template__c e : trigger.new)
    {
        RuleID.add(e.GDPR_Rule__c);
    }
    
    List<Contact> con = [SELECT FirstName, LastName, Email FROM Contact WHERE Id IN (SELECT Contact__c FROM Assigned_Communication_Rule__c WHERE OptinCommunicationMethod__c IN : RuleID )];
    
	for (contact c : con)
    {
        for (Email_Template__c em : trigger.new)
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c.Email}); 
            mail.setSubject(em.Subject__c); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Hello'+ c.FirstName+' ' + c.LastName+', <br><br> '+em.Email_Body__c+ ' <br> Regards, <br><br> Admin Team'); 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
        }
    }
}

Test Class
 
@isTest
public class Test_FireEmailWithEmailTemplate {
    
    static testMethod void EmailTemplate1()
    {
      
        Contact c = new Contact ();
        c.LastName = 'test';
        c.FirstName = 'test';
        c.Email = 'test@test.com';
        insert c;
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
        mail.setToAddresses(new String[] {c.Email}); 
        mail.setSubject(em.Subject__c); 
        mail.setUseSignature(false);
        mail.setHtmlBody('Hello'+ c.FirstName+' ' + c.LastName);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
        
    }
    
    
}

 
Hello,
Help me to get 100% code coverage of this code,
 
public class Overview_Controller {
    
    public string baseurl = URL.getSalesforceBaseUrl().toExternalForm();
    public string create = '/one/one.app#/alohaRedirect/'; 
    public string Framework = '/one/one.app#/sObject/CGR__CG_Framework__c/list';

    
    public PageReference CreateFramework(){
        if(UserInfo.getUiTheme()== 'Theme4d')
        {
            string FrameworkPrefix = CGR__CG_Framework__c.sObjectType.getDescribe().getKeyPrefix();
            PageReference  p = new PageReference (baseurl+create+FrameworkPrefix+'/e' );
            p.setRedirect(true);
            return p;
        }
		
		else
        {
            string FPre = CGR__CG_Framework__c.sObjectType.getDescribe().getKeyPrefix();
            PageReference p1 = new PageReference ('/'+FPre+'/e' );
            p1.setRedirect(true);
            return p1;
        }
	}
}

I tried this test class, but it only covers else part. it does not cover if (condition) block. 
 
@isTest
public class Overview_Controller_Test {
    
    private static testMethod void testOverview()
    {
        Overview_Controller oc = new Overview_Controller ();
        
        oc.CreateFramework();
    	
	}
}

Help me to achieve 100% code coverage.

--
Thank-You!
Krutarth Soni