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
Veerendar AellaVeerendar Aella 

Need 75 plus test coverage for the apex trigger

Hi All,

I need a test coverage of 75% plus for the below trigger, Please help.

trigger example on Lead (before insert, before update) 
{
   system.debug('In Assign Contractor 2.1');
   static string pcode;

   
  

    Map<Id, String> mLedvsZip   = new Map<Id, String>();
    Map<String, List<Id>> mZipvsLeads = new Map<String, List<Id>>(); 
    
    system.debug(Trigger.new);
    
    for(Lead led: Trigger.new){
    
        system.debug('Trigger New loop:');
        
        if(led.PostalCode <> null && led.PostalCode.length() >= 5) {
          pcode = led.PostalCode.substring(0,5);
        }
        
        
        if(led.CTrigger__c && led.WG_Status__c == 'Gas Available'){
            system.debug('In led.ctrigger loop');
            system.debug('In led.PostalCode: ' + led.PostalCode );
            system.debug('In led.Contractor: ' + led.Contractor__c);
            
            if( led.Contractor__c == null && String.isNotBlank(led.PostalCode)){
            
                
                
                system.debug('initial zip: ' + led.PostalCode);
                system.debug('truncated zip: ' + pcode);
                 
                if(led.PostalCode.length() >= 5) {
                system.debug('mLedvsZip Put: ' + pcode);
                mLedvsZip.put(led.Id, pcode);
                }
                else
                {
                system.debug('>>>>>> invalid zip found: ' + led.PostalCode);
                }
                
                if(!mZipvsLeads.isEmpty() && mZipvsLeads.containsKey(pcode)){
                    List<Id> tempIds = mZipvsLeads.get(pcode);
                    tempIds.add(led.Id);
                    mZipvsLeads.put(pcode, tempIds);
                }
                else if(!mZipvsLeads.isEmpty() && !mZipvsLeads.containsKey(pcode))
                    mZipvsLeads.put(pcode, new List<Id>{led.Id});
                else if(mZipvsLeads.isEmpty())
                    mZipvsLeads.put(pcode, new List<Id>{led.Id});
            }
        }
    }//End For
    
    if(!mZipvsLeads.isEmpty()){
        List<Zip_Code__c> lstZC = [SELECT Zip_Code__c,  
                                          Name, Account__c, 
                                          Account__r.Last_Lead_Assign_Date__c 
                                   FROM Zip_Code__c 
                                   WHERE Zip_Code__c IN: mZipvsLeads.keySet() AND 
                                         Account__r.Accept_Leads__c = true  
                                   ORDER BY Account__r.Last_Gas_Available_Lead_Assigned__c ASC NULLS FIRST];                                                           
        system.debug('List from SQL: ' + lstZC);
        if(!lstZC.isEmpty()){
            Map<String, List<Id>> mZCvAccs = new Map<String, List<Id>>();//Zip vs associatd Accounts
            for(Zip_Code__c zc: lstZC){
                if(!mZCvAccs.isEmpty() && mZCvAccs.containsKey(zc.Zip_Code__c)){
                    List<Id> tempIds = mZCvAccs.get(zc.Zip_Code__c);
                    tempIds.add(zc.Account__c);
                    mZCvAccs.put(zc.Zip_Code__c, tempIds);
                }
                else if(!mZCvAccs.isEmpty() && !mZCvAccs.containsKey(zc.Zip_Code__c))
                    mZCvAccs.put(zc.Zip_Code__c, new List<Id>{zc.Account__c});
                else if(mZCvAccs.isEmpty())
                    mZCvAccs.put(zc.Zip_Code__c, new List<Id>{zc.Account__c});
            }
            
            if(!mZipvsLeads.isEmpty() && !mZCvAccs.isEmpty()){
                Map<Id, DateTime> mAcctoUpdate = new Map<Id, DateTime>();//Account to Update for Datetime of Assignement
                for(String zip: mZipvsLeads.keySet()){
                    List<Id> lstLeads    = mZipvsLeads.get(zip); 
                    List<Id> lstAccounts = mZCvAccs.get(zip); 
                    
                    if(lstLeads.size()==1){
                        if(!lstAccounts.isEmpty() && lstAccounts.size() == 1){
                            if(Trigger.isInsert)
                                trigger.new[0].Contractor__c = lstAccounts[0];
                            else if(Trigger.isUpdate)
                                trigger.newMap.get(lstLeads[0]).Contractor__c = lstAccounts[0];                        
                            mAcctoUpdate.put(lstAccounts[0], DateTime.now());
                        }
                        else if(!lstAccounts.isEmpty() && lstAccounts.size() > 1){
                            if(Trigger.isInsert)
                                trigger.new[0].Contractor__c = lstAccounts[0];
                            else if(Trigger.isUpdate)
                                trigger.newMap.get(lstLeads[0]).Contractor__c = lstAccounts[0];
                            mAcctoUpdate.put(lstAccounts[0], DateTime.now());
                        }
                    }
                    else if(lstLeads.size()>1){
                        if(!lstAccounts.isEmpty() && lstAccounts.size() == 1){
                            for(Integer i=0; i<lstLeads.size(); i++){
                                if(Trigger.isInsert)
                                    trigger.new[i].Contractor__c = lstAccounts[0];
                                else if(Trigger.isUpdate)
                                    trigger.newMap.get(lstLeads[i]).Contractor__c = lstAccounts[0];
                                mAcctoUpdate.put(lstAccounts[0], DateTime.now());
                            }
                        }
                        else if(!lstAccounts.isEmpty() && lstAccounts.size() > 1){
                            if(lstLeads.size() <= lstAccounts.size()){
                                for(Integer i=0; i<lstLeads.size(); i++){
                                    if(Trigger.isInsert)
                                        trigger.new[i].Contractor__c = lstAccounts[0];
                                    else if(Trigger.isUpdate)
                                        trigger.newMap.get(lstLeads[i]).Contractor__c = lstAccounts[0];
                                    mAcctoUpdate.put(lstAccounts[i], DateTime.now());
                                }
                            }
                            else if(lstLeads.size() > lstAccounts.size()){
                                List<List<Id>> lstlstId = Util.CreatePackets(lstAccounts.size(), lstLeads);
                                for(Integer i=0; i<lstlstId.size(); i++){
                                    for(Integer j=0; j<lstlstId[i].size(); j++){
                                        trigger.newMap.get(lstlstId[i][j]).Contractor__c = lstAccounts[j];
                                        mAcctoUpdate.put(lstAccounts[i], DateTime.now());                                       
                                    }
                                }
                            }
                        }
                    }
                }
                if(!mAcctoUpdate.isEmpty()){
                    List<Account> lstAccUp = new List<Account>();
                    for(Id accId: mAcctoUpdate.keySet())
                        lstAccUp.add(new Account(id= accId, 
                                                 Last_Gas_Available_Lead_Assigned__c = mAcctoUpdate.get(accId)));
                    if(!lstAccUp.isEmpty())
                        update lstAccUp;
                }
            }
        }
    }
    
 //   Update Contractor Email
 
 Set<id> acctIds = new Set<id>();
 system.debug('Trigger NEW: '+ Trigger.new);

 for(Lead led: trigger.new){
     // Lead oldLead = Trigger.oldMap.get(led.ID);
     acctIds.add(led.Contractor__c);
     }
  system.debug('Acctids: '+ acctids);  
    
 // create a map so that Account is locatable by its Id (key)  
  
    Map<id, account> acctsMap = new Map<id, account>(
[SELECT Id, Partner_Email__c,Partner_Secondary_Email__c  FROM Account WHERE id IN :acctIds]);

system.debug('AcctsMap: ' + acctsMap);
  
 for (Lead led : Trigger.new) { 
 
           if(led.CTrigger__c && !led.Contractor_Contact__c){
           // led.CTrigger__c = False;
           }
 
           if (led.Contractor__c != null){
             led.Contractor_Email__c = acctsMap.get(led.Contractor__c).Partner_Email__c;
             led.Contractor_Secondary_Email__c = acctsMap.get(led.Contractor__c).Partner_Secondary_Email__c;
             led.Contractor_Assignment_Status__c = 'Assigned';
            }
           else
           {
             led.Contractor_Email__c = '';
             led.Contractor_Secondary_Email__c = '';
             if(led.CTrigger__c) {
                led.Contractor_Assignment_Status__c = 'No Contractor Found';}
             else
                {
                led.Contractor_Assignment_Status__c = '';
                }
           }
    }
    
     
}
Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi veerendar,

In your Trigger you are the adding the Lead Id and pcode value in a Map.
if(led.PostalCode.length() >= 5) {
                system.debug('mLedvsZip Put: ' + pcode);
                mLedvsZip.put(led.Id, pcode);
}
But in the below line you are checking whether the mZipvsLeads Map Cotains the Key pcode or not.This Condition will always be false Right?Can you give your Suggestion on this.

if(!mZipvsLeads.isEmpty() && mZipvsLeads.containsKey(pcode))

Thanks
Dinesh