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
Sangeetha TSangeetha T 

Test class for apex trigger for code coverage while production migration

Need test class for the following trigger with atleast 75 code coverage 
Trigger 1
The below trigger  : Lead and Contact have same checkbox fields, both object fields should have same value 
trigger checktheLeadCheckBox on Contact (after update) 
{
  for(Contact c : Trigger.New)
  {
    List<Lead> li = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Lead where Email =: c.Email AND IsConverted =: false]; 
    for(Lead l : li)
    {
     If(l.Id != null)
     { 
       l.Customer_of_PubKCyber__c=c.Customer_of_PubKCyber__c;
       l.Customer_of_PubKLaw__c = c.Customer_of_PubKLaw__c;
       update l;
     }
    }
  }
}
Trigger 2 
The below trigger : checking the value of the checkboxes on Contacts object based on a condition 
trigger updatecontactcheckbox on Opportunity (after insert, after update) 
{    
    for(Opportunity o : Trigger.New)
    {
  
     if( o.Contact__c != null)                 
     {      
       Contact c = [select Id, Customer_of_PubKCyber__c,Email,Customer_of_PubKLaw__c from Contact where Id =: o.Contact__c ]; 
       
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKCyber Newsletter'))
    {
         c.Customer_of_PubKCyber__c = True;
        
    }
    else if(o.Product_Name__c == 'PubKCyber Newsletter')
         { 
           c .Customer_of_PubKCyber__c = false;
           
         }
          
if((o.Subscription_Start_Date__c <= System.Today()) && (o.Subscription_End_Date__c >= System.Today()) && (o.Product_Name__c == 'PubKLaw Newsletter'))
        { 
          c.Customer_of_PubKLaw__c= True;
         
        }
       else if(o.Product_Name__c == 'PubKLaw Newsletter')
         { 
           c.Customer_of_PubKLaw__c = false;            
         }
       update c; 
      }   
     }  
  }

Please give me the test class to cover 75 of code coverage to migration.......Very Critical
Please give me the test class to cover 75 of code coverage to migration.......Very Critical
Nethaji BaskarNethaji Baskar
@Sangeetha,
First thing this trigger you have given is not bulkified... You might get a SOQL Governor Limit 101 error if more than 100 records are bulk updated in yourr org.

Issues in the code:
1) You have SOQL query inside a For loop (line no 3 in ur first trigger)
2) Checking for ID field not null is not required, since ID field will always be created when a Salesforce record is saved in database. (line no 8)
3) You have a DML statement inside For loop (line no 12)

Refer this Article for more info https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_limits_intro.htm

I have bulkified your First trigger as below...
trigger checktheLeadCheckBox on Contact (after update) 
{
  Set<String> contactEmails = new Set<String>();
  Map<String,List<Boolean>> contactFieldsMap = new Map<String, List<boolean>>(); 
  for(Contact c : Trigger.New)
  {
    contactEmails.add(c.Email);
    contactFieldsMap.put(c.Email, new List<Boolean>());
    contactFieldsMap.get(c.Email).add(c.Customer_of_PubKCyber__c);
    contactFieldsMap.get(c.Email).add(c.Customer_of_PubKLaw__c);
  }
  
  List<Lead> li = new List<lead>();
    for(Lead l : [select Id, Customer_of_PubKCyber__c,Customer_of_PubKLaw__c,Email  from Lead where Email =: contactEmails AND IsConverted = false])
    {
       if(contactFieldsMap.containskey(l.Email)) {
         
         List<Boolean> contactFieldValues = contactFieldsMap.get(l.Email);
         l.Customer_of_PubKCyber__c = contactFieldValues[0];
         l.Customer_of_PubKLaw__c = contactFieldValues[1];
         li.add(l); 
       }
       
     }
   if(li.size() > 0) {
       try{
         update li;
       } catch(dmlexception e) {
       
       }
       
       }  
     
 }


// The below Test Class for the Above Trigger will give you 95% code coverage.

/**
 * Test Class for checktheLeadCheckBox
 */
@isTest 
public with sharing class checktheLeadCheckBox_Test {

// Test Method
public static testmethod void  testChangePasswordController() {

  Contact newContact = new Contact(lastName = 'TestContact', Email ='test@test.com');
  insert newContact;

  Lead newLead = new Lead(lastName = 'TestLead' , Email = 'test@test.com', Company ='TestCompany');
  insert newLead;
  
  newContact.Customer_of_PubKCyber__c = true;
  newContact.Customer_of_PubKLaw__c = true;
  
  update newContact;
                               
    }    
}
Learn Salesforce Test Class through Salesforce documentation or Videos for more info..
Example... https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Try to modify your other trigger similar to the given sample bulkified trigger and write your own test classes..

Good Luck..!

-Nethaji
 
BWSBWS
@Nethaji Baskar,
Good job explaining the "what" and "why" vs just showing "how" by providing only code.
Nethaji BaskarNethaji Baskar
Thanks BWS!