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
P KumarP Kumar 

Test class is not achieving 100% Code Coverage for follwoing Trigger

Can anyone please let me know, why am not able to get 100% code coverage.

Trigger
======

// Trigger for APAC Region - to find Lead & Contact Duplicate

trigger APACLeadEventDedupe on Lead (before insert, before update) {

Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;

Set<String> allLeadEmails = new Set <String> ();
Set<String> allFirstNames = new Set <String> ();
Set<String> allLastNames = new Set <String> ();
Set<String> allCompanyCodes = new Set <String> ();
Set<String> APACCompanyCodes = new Set<String>{'AU1', 'SG1', 'IN1', 'NZ1', 'CN1'};

       
            if(newlead.email != null && newlead.FirstName != null && newlead.LastName != null && newlead.Company_Code__c != null && newlead.RecordTypeId == rtId ) {
                allLeadEmails.add(newlead.email);
                allFirstNames.add(newlead.FirstName);
                allLastNames.add(newlead.LastName);
                allCompanyCodes.add(newlead.Company_Code__c);
                }
    }

//Step 2: Query for All Contact Records in Step 1


    List<Contact> ContactList = [Select Id ,Email,FirstName,LastName,Company__c,Account_Number__c,PIN__c,Ship_To__c,
                                            Created_Date__c,Primary_Account_Owner__c,Primary_Account_Owner_ID__c,Primary_Sales_Function__c,
                                            First_Order_Date__c,OwnerId,Owner.Name,Turnover__c
                                            FROM Contact WHERE Email IN: allLeadEmails AND FirstName IN: allFirstNames AND LastName IN: allLastNames AND Company__c IN: allCompanyCodes]; // AND RecordTypeId=:ContactRtId];
                                            
    System.debug('After query  -values in Where Clause   allLeadEmails '+allLeadEmails );  
          System.debug('After query  -values in Where Clause   allFirstNames '+allFirstNames );  
                System.debug('After query  -values in Where Clause   allLastNames '+allLastNames );
                           System.debug('After query  -values in Where Clause   allCompanyCodes '+allCompanyCodes );  
  
      System.debug('After query  - ContactList '+ContactList );                                      

//For the triggers in the loop, add the relevant contact information.

        
  for (Lead LeadInLoop : Trigger.new) {
      System.debug('---> Line No : 53 <---');
      System.debug('---> Line No : 54 <---'+ContactList);
     for (Contact c: ContactList){
          String companyCode = LeadInLoop.Company_Code__c;
          System.debug('******Company Code is : '+ LeadInLoop.Company_Code__c );
          if(APACCompanyCodes.contains(LeadInLoop.Company_Code__c)){
          System.debug('******Company Code Inserted as : '+ LeadInLoop.Company_Code__c );
          if((LeadInLoop.email == c.email) && (LeadInLoop.FirstName == c.FirstName) && (LeadInLoop.LastName == c.LastName) ){

                 LeadInLoop.Existing_Contact__c = TRUE;
                 LeadInLoop.Contact_Created_Date__c = c.Created_Date__c;
                 LeadInLoop.Contact_ID__c = c.Id;
                 LeadInLoop.Account_Number__c = c.Account_number__c;
                 LeadInLoop.Ship_To__c = c.Ship_To__c;
                 LeadInLoop.Contact_Company__c = c.Company__c;
                 LeadInLoop.Primary_Account_Owner__c = c.Primary_Account_Owner__c;
                 LeadInLoop.Primary_Account_Owner_ID__c = c.Primary_Account_Owner_ID__c;
                 LeadInLoop.Contact_Primary_Sales_Function__c = c.Primary_Sales_Function__c;
                 LeadInLoop.First_Order_Date__c = c.First_Order_Date__c;
                 LeadInLoop.Contact_Owner_ID__c = c.Owner.Name;
                 LeadInLoop.Turnover__c = c.Turnover__c;
                 LeadInLoop.PIN__c = c.PIN__c;
                 
              if(LeadInLoop.Lead_Created_Date__c > c.Created_Date__c){
                  if(LeadInLoop.LeadSource == 'Supplier Lead Referral'){
                      LeadInLoop.OwnerId = c.OwnerId;
                  }
              else{
                LeadInLoop.addError('Lead already exist in Contact, Ref ID : ' + c.Id);
              }
            }
            if(LeadInLoop.Lead_Created_Date__c <= c.Created_Date__c){
                if(c.Turnover__c>0.00){
                    LeadInLoop.Status = 'Order Placed';
                    LeadInLoop.Ready_to_Convert__c = TRUE;
                }
                if((c.Turnover__c <= 0.00 || c.Turnover__c== null) && (LeadInLoop.Country_code__c != 'India' || LeadInLoop.Country_code__c != 'China' || LeadInLoop.Country_code__c != 'Taiwan' || LeadInLoop.Country_code__c != 'Hong Kong'))
                       LeadInLoop.Status = 'Account created';
                }
          }
      }
    }
  }
}


Test Class
========

@isTest(SeeAllData=true)

public class APACLeadEventDedupeTest{

public static testMethod void APACLeadEventDedupeStaticTestMethod(){

Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;
Id SiteID= [select Id,name from RecordType where name ='Site' limit 1].Id;
Id BillRtId = [Select Id, name from RecordType where name = 'Billing Account' limit 1].Id;
User O = [select id from user where id='00520000001Omvk' LIMIT 1];
Set<String> allLeadEmails = new Set <String> ();
Set<String> allFirstNames = new Set <String> ();
Set<String> allLastNames = new Set <String> ();
Set<String> allCompanyCodes = new Set <String> ();
Set<String> APACCompanyCodes = new Set<String>{'AU1', 'SG1', 'IN1', 'NZ1', 'CN1'};

try{
//    Test.startTest();
  {
      Account Billing = 
      new Account (
          Name='TestBillingAcc', 
          RecordTypeId=BillRtId, 
          Company__c='SG1',
          Primary_Account_Owner__c='Katherine Schuil',
          Primary_Account_Owner_ID__c='TOFFKA',
          Primary_Sales_Function__c='TeleMarketing',
          Owner = O);
      
//      insert Billing;
    
    System.debug('**Billing Account Created' + Billing.Name);
    
      Account Site = 
      new Account (
          Name='TestSiteAcc', 
          RecordTypeId=SiteId, 
          Company__c='SG1', 
          Ship_To__c = '001',
          Primary_Account_Owner__c='Katherine Schuil',
          Primary_Account_Owner_ID__c='TOFFKA',
          Primary_Sales_Function__c='TeleMarketing',
          Owner = O,
          Parent = Billing,
          Specific_Customer_Type__c='Other',
          CEM_OEM_Tier__c=1,
          Fixed_Quote_Comments__c='Testing Fixed Quote Comments');
      
//      insert Site;

    System.debug('**Site Account Created' + Site.Name);

      Contact c = 
      new Contact (
          FirstName = 'Test',
          LastName = 'Lead',
          Email = 'testinglead@element14.com',
          Account_Number__c = '12345',
          PIN__c = '064582',
          First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
          Turnover__c = 10000.00,
          Account = Site
          );
      
//      insert c;
  
      System.debug('**Contact Created' + c.FirstName );
  
      Lead l = 
      new Lead (
          FirstName ='Test', 
          LastName ='Lead', 
          Country_code__c ='Singapore',
          Company = 'SG1',
          email = 'testinglead@element14.com',
          Lead_Type__c = 'New Company',
          LeadSource = 'Supplier Lead Referral',
      RecordTypeId=rtID,
          Lead_Source_Detail_NEW__c = '3M');
          
//          insert l;
          
          Test.startTest();{insert Billing;insert Site;insert c;insert l;}
          
          
          System.debug('**Lead Created : ' + l.FirstName + ' ' + l.LastName);
          System.debug('**Lead Created : ' + l.Company_Code__c);
          
          List<Contact> ContactList = [Select Id ,Email,FirstName,LastName,Company__c,Account_Number__c,PIN__c,Ship_To__c,
                                            Created_Date__c,Primary_Account_Owner__c,Primary_Account_Owner_ID__c,Primary_Sales_Function__c,
                                            First_Order_Date__c,OwnerId,Owner.Name,Turnover__c
                                            FROM Contact WHERE Email IN: allLeadEmails AND FirstName IN: allFirstNames AND LastName IN: allLastNames AND Company__c IN: allCompanyCodes AND Id =: c.Id];
                                            
          List<Lead> NewLead = [Select Id, FirstName,  LastName, Company_Code__c, email, Existing_Contact__c,Contact_Created_Date__c,
                                          Contact_ID__c, Account_Number__c, Ship_To__c, Contact_Company__c, Primary_Account_Owner__c,
                                          Primary_Account_Owner_ID__c, Contact_Primary_Sales_Function__c, First_Order_Date__c, Contact_Owner_ID__c,
                                          Turnover__c, PIN__c FROM Lead where Id =: l.Id];
//For the triggers in the loop, add the relevant contact information.

  for (Lead LeadInLoop : NewLead) {
      for (Contact c1: ContactList){
      System.debug('Company Code in Lead Loop'+LeadInLoop.Company_Code__c);
      if(APACCompanyCodes.contains(LeadInLoop.Company_Code__c)){
      
          if((LeadInLoop.email == c1.email) && (LeadInLoop.FirstName == c1.FirstName) && (LeadInLoop.LastName == c1.LastName) ){

                 LeadInLoop.Existing_Contact__c = TRUE;
                 LeadInLoop.Contact_Created_Date__c = c1.Created_Date__c;
                 LeadInLoop.Contact_ID__c = c1.Id;
                 LeadInLoop.Account_Number__c = c1.Account_number__c;
                 LeadInLoop.Ship_To__c = c1.Ship_To__c;
                 LeadInLoop.Contact_Company__c = c1.Company__c;
                 LeadInLoop.Primary_Account_Owner__c = c1.Primary_Account_Owner__c;
                 LeadInLoop.Primary_Account_Owner_ID__c = c1.Primary_Account_Owner_ID__c;
                 LeadInLoop.Contact_Primary_Sales_Function__c = c1.Primary_Sales_Function__c;
                 LeadInLoop.First_Order_Date__c = c1.First_Order_Date__c;
                 LeadInLoop.Contact_Owner_ID__c = c1.Owner.Name;
                 LeadInLoop.Turnover__c = c1.Turnover__c;
                 LeadInLoop.PIN__c = c1.PIN__c;
                 
              if(LeadInLoop.Lead_Created_Date__c > c1.Created_Date__c){
                  if(LeadInLoop.LeadSource == 'Supplier Lead Referral'){
                      LeadInLoop.OwnerId = c1.OwnerId;
                  }
              else{
                LeadInLoop.addError('Lead already exist in Contact, Ref ID : ' + c1.Id);
              }
            }
            
            System.debug('******@ After Lead Check *****');
            
            if(LeadInLoop.Lead_Created_Date__c <= c1.Created_Date__c){
                if(c1.Turnover__c>0.00){
                    LeadInLoop.Status = 'Order Placed';
                    LeadInLoop.Ready_to_Convert__c = TRUE;
                }
                
                System.debug('******@ Lead Status Update - Order Placed*****');
                
                if((c1.Turnover__c <= 0.00 || c1.Turnover__c== null) && (LeadInLoop.Country_code__c != 'India' || LeadInLoop.Country_code__c != 'China' || LeadInLoop.Country_code__c != 'Taiwan' || LeadInLoop.Country_code__c != 'Hong Kong'))
                       LeadInLoop.Status = 'Account created';
                }
                
                System.debug('******@ Lead Status Update - Account Created*****');
          }
      }
      }
  }
 }

} catch (Exception e) {
    system.assertEquals('********* Error Found ********', e.getMessage());
    System.debug('******@ Exception*****');

 Test.stopTest();
 System.debug('******End of Test*****');
}

}
Best Answer chosen by P Kumar
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Pranava,

Please satisfy below query condition for contacts in your your test class.
List<Contact> ContactList = [Select Id ,Email,FirstName,LastName,Company__c,Account_Number__c,PIN__c,Ship_To__c,
                                            Created_Date__c,Primary_Account_Owner__c,Primary_Account_Owner_ID__c,Primary_Sales_Function__c,
                                            First_Order_Date__c,OwnerId,Owner.Name,Turnover__c
                                            FROM Contact WHERE Email IN: allLeadEmails AND FirstName IN: allFirstNames AND LastName IN: allLastNames AND Company__c IN: allCompanyCodes]; // AND RecordTypeId=:ContactRtId]

Please try below code for contact creation.
 
Contact c = 
      new Contact (
          FirstName = 'Test',
          LastName = 'Lead',
          Email = 'testinglead@element14.com',
          Account_Number__c = '12345',
          PIN__c = '064582',
          First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
          Turnover__c = 10000.00,
          Account = Site,       
          Company__c ='Singapore',

          );
Let us know if it helps you.

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Pranava Kumar Bachu,

Could you please highlight ,which parts of your class are not covered ?

 
P KumarP Kumar
Hi Ashish Sharma,

From '//Step 2: Query for All Contact Records in Step 1' its not covered.

Regards,
Pranav
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Pranava,

Please satisfy below query condition for contacts in your your test class.
List<Contact> ContactList = [Select Id ,Email,FirstName,LastName,Company__c,Account_Number__c,PIN__c,Ship_To__c,
                                            Created_Date__c,Primary_Account_Owner__c,Primary_Account_Owner_ID__c,Primary_Sales_Function__c,
                                            First_Order_Date__c,OwnerId,Owner.Name,Turnover__c
                                            FROM Contact WHERE Email IN: allLeadEmails AND FirstName IN: allFirstNames AND LastName IN: allLastNames AND Company__c IN: allCompanyCodes]; // AND RecordTypeId=:ContactRtId]

Please try below code for contact creation.
 
Contact c = 
      new Contact (
          FirstName = 'Test',
          LastName = 'Lead',
          Email = 'testinglead@element14.com',
          Account_Number__c = '12345',
          PIN__c = '064582',
          First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
          Turnover__c = 10000.00,
          Account = Site,       
          Company__c ='Singapore',

          );
Let us know if it helps you.
This was selected as the best answer