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
bheemudu neelibheemudu neeli 

Need a help on test class creation to cover the execute method for the batch class

Hi All,
I am unable to cover the code for Execute method for the below batch class.
can some can please help.
.......................
global class DuplicateContactsMergeProcess implements Database.Batchable<sobject>, Database.Stateful
{  
    
   
    //Execute method
    global void execute (Database.BatchableContext BC, List<Contact> scope)
    {
        List<Contact> target=[select id,FirstName,LastName,Active__c,FN__c,LN__c,ReportsToid,Description,Title__c,OwnerId,Extension__c,Swift__c,Team__c,External__c,Weblead__c,Can_be_contacted_for_Marketing_purposes__c,Preferred_Communication_Channel__c,Group_Phone_Number__c,Product_Families__c,Services__c,Subscriptions__c,Survey__c,DoNotCall,HasOptedOutOfEmail,AssistantName,AssistantPhone,Contact_for__c,Entity__c,LastProTime__c,AccountId,OtherCountry,FederationIdentifier__c,OtherLatitude,OtherLongitude,OtherGeocodeAccuracy,salutation,OtherStreet,OtherCity,OtherState,OtherPostalCode,MailingCountry,MailingLatitude,MailingLongitude,MailingGeocodeAccuracy,MailingStreet, MailingCity,MailingState,MailingPostalCode,Email,ApexBatchID__c, Title, Role__c, Function__c, Language__c, Group_email__c, Phone, MobilePhone, Fax, Switchboard__c  from Contact where   AccountId<>null  AND recordtype.name!=:RecName  AND LastProTime__c!=:BNow and createddate=today  limit 1 ];
        system.debug('## Preparing to merge '+target); 
        if(target.size()>0)
        {
            String sFN=(String)target[0].get('FN__c');   
            String sLN=(String)target[0].get('LN__c'); 
            String sEmail=(String)target[0].get('Email'); 
            Id accId=(ID)target[0].get('AccountId'); 
            Id conId=(ID)target[0].get('Id');
            try{
                Contact[] lstContact=[Select id,FirstName,LastName,FN__c,LN__c,ReportsToid,Description,Active__c,Title__c,OwnerId,LastProTime__c,Extension__c,Swift__c,Team__c,External__c,Weblead__c,Can_be_contacted_for_Marketing_purposes__c,Preferred_Communication_Channel__c,Group_Phone_Number__c,Product_Families__c,Services__c,Subscriptions__c,Survey__c,DoNotCall,HasOptedOutOfEmail,AssistantName,AssistantPhone,Contact_for__c,Entity__c,FederationIdentifier__c,ApexBatchID__c,AccountId,OtherCountry,OtherLatitude,OtherLongitude,OtherGeocodeAccuracy,salutation,OtherStreet,OtherCity,OtherState,OtherPostalCode,MailingCountry,MailingLatitude,MailingLongitude,MailingGeocodeAccuracy,MailingStreet, MailingCity,MailingState,MailingPostalCode,Email ,Title,Role__c,Function__c, Language__c, Group_email__c, Phone, MobilePhone, Fax, Switchboard__c  from Contact where  AccountId<>null  AND FN__c=:sFN and LN__c=:sLN and AccountId=:accId and Email=:sEmail and id!=:conId   limit 1];
                system.debug('## List of identified contacts '+lstContact); 
                system.debug('## '+lstContact.size()); 
                if(lstContact.size()>0)
                {
                    List<User> UserDup =[select id, contactid, isactive from user where contactid=:lstContact[0].ID limit 1];    
                    List<User> Usertarg =[select id, contactid, isactive from user where contactid=:target[0].ID limit 1];    
                    system.debug('## UserDup users '+UserDup);
                    system.debug('## Usertarg users '+Usertarg);
                    List<Contact> us1 = [Select id, Name, Createddate from Contact where Id=:target[0].Id];
                    List<Contact> use1 = [Select id, Name, Createddate from Contact where Id=:lstContact[0].Id];
                     Mdate=us1[0].Createddate; 
                     Cdate=use1[0].Createddate;
                    System.debug('master createddate'+Mdate);
                    System.debug('Child createddate'+Cdate);
                    if(Usertarg.size()>0 && UserDup.size()>0)
                    {
                        system.debug('## Portal User Size > Zero');
                        If((UserDup[0].IsActive==TRUE && Usertarg[0].IsActive==TRUE)||(UserDup[0].IsActive==False && Usertarg[0].IsActive==False))
                        {
                            system.debug('## Portal User Check');
                            IF(Mdate>Cdate)
                            {
                                MasterContact=lstContact[0];
                                finalmaster=lstContact[0].Id;
                                otherContact=target[0];
                                finalcontact=target[0].id;
                                system.debug('## MasterContact '+MasterContact);
                                system.debug('## otherContact '+otherContact);
                            }
                            IF(Mdate<Cdate)
                            {
                                otherContact=lstContact[0];
                                finalcontact=lstContact[0].Id;
                                MasterContact =target[0];
                                finalmaster =target[0].id;
                                system.debug('## MasterContact '+MasterContact);
                                system.debug('## otherContact '+otherContact);
                            }
                        }
                        If(UserDup[0].IsActive==TRUE && Usertarg[0].IsActive==FALSE){
                        MasterContact=lstContact[0];
                        finalcontact=target[0].id;
                        otherContact=target[0];
                        finalmaster=lstContact[0].Id;
                        system.debug('## Master Contact '+MasterContact); 
                        system.debug('## deprecated Contact '+otherContact); 
                            
                        }
                        If(UserDup[0].IsActive==FALSE && Usertarg[0].IsActive==TRUE){
                        otherContact=lstContact[0];
                        finalmaster=target[0].id;
                        MasterContact=target[0];
                        finalcontact=lstContact[0].Id;
                        system.debug('## Master Contact '+MasterContact); 
                        system.debug('## deprecated Contact '+otherContact);
                        }
                    }
                    if(Usertarg.size()==0 && UserDup.size()>0)
                    {
                        MasterContact=lstContact[0];
                        finalcontact=target[0].id;
                        otherContact=target[0];
                        finalmaster=lstContact[0].Id;
                        system.debug('## Master Contact '+MasterContact); 
                        system.debug('## deprecated Contact '+otherContact); 
                    }
                    if(Usertarg.size()>0 && UserDup.size()==0)
                    {
                        otherContact=lstContact[0];
                        finalmaster=target[0].id;
                        MasterContact=target[0];
                        finalcontact=lstContact[0].Id;
                        system.debug('## Master Contact '+MasterContact); 
                        system.debug('## deprecated Contact '+otherContact); 
                    }
                    if(Usertarg.size()==0 && UserDup.size()==0)
                    {
                        IF(Mdate>Cdate)
                        {
                            MasterContact=lstContact[0];
                            finalmaster=lstContact[0].Id;
                            otherContact=target[0];
                            finalcontact=target[0].id;
                            system.debug('## MasterContact '+MasterContact);
                            system.debug('## otherContact '+otherContact);
                        }
                        IF(Mdate<Cdate)
                        {
                            MasterContact=target[0];
                            finalmaster=target[0].id;
                            otherContact=lstContact[0];
                            finalcontact=lstContact[0].Id;
                            system.debug('## MasterContact '+MasterContact);
                            system.debug('## otherContact '+otherContact);
                        }
                    }
                    system.debug('## Master Contact '+MasterContact); 
                    system.debug('## deprecated Contact '+otherContact); 
                    
                    if(otherContact!=NULL && MasterContact!=NULL)
                    { 
                                                
                        if(otherContact.Function__c!=null)
                        {
                            MasterContact.Function__c =otherContact.Function__c;
                        }
                        if(otherContact.Title!=null)
                        {
                            MasterContact.Title =otherContact.Title;
                        }
                        
                        if(otherContact.Role__c!=null)
                        {
                            MasterContact.Role__c =otherContact.Role__c;
                        }
                        if(otherContact.Language__c!=null)
                        {
                            MasterContact.Language__c =otherContact.Language__c;
                        }
                        if(otherContact.Email!=null)
                        {
                            MasterContact.Email =otherContact.Email;
                        }
                        if(otherContact.Group_email__c!=null)
                        {
                            MasterContact.Group_email__c =otherContact.Group_email__c;
                        }
                        if(otherContact.Phone!=null)
                        {
                            MasterContact.Phone =otherContact.Phone;
                        }
                       
                         List<Contact> us = [Select id, Name,owner.isactive, Owner.Profile.Name from Contact where Id=:MasterContact.Id];
                         List<Contact> use = [Select id, Name,owner.isactive, Owner.Profile.Name from Contact where Id=:otherContact.Id];
                         system.debug('## MasterContact.Owner ##'+us[0].Owner.profile.name);
                         system.debug('## otherContact.Owner ##'+use[0].Owner.profile.name);
                         mergeprofile=Us[0].Owner.profile.name;
                         listprofile=use[0].Owner.profile.name;
                         system.debug('## MasterContact.Owner ##'+mergeprofile);
                         system.debug('## otherContact.Owner ##'+listprofile);   
                        system.debug('## Owner isactive##'+use[0].owner.isactive+'master own'+Us[0].owner.isactive);
                       // if(use[0].owner.isactive==true){
                        //     MasterContact.OwnerId =otherContact.OwnerId; 
                        //}
                                       
                if((listprofile=='External access' || listprofile=='PSS User' || listprofile=='Rollout profile' || listprofile=='Rollout profile EUI' || listprofile=='Rollout profile France' || listprofile=='Rollout profile Unit Head')
                && (mergeprofile!='External access' && mergeprofile!='PSS User' && mergeprofile!='Rollout profile' && mergeprofile!='Rollout profile EUI' && mergeprofile!='Rollout profile France' && mergeprofile!='Rollout profile Unit Head'))  
                 {  
                       if(use[0].owner.isactive==true){ 
                             MasterContact.OwnerId =otherContact.OwnerId; 
                        }
                   
               } 
                     MasterContact.ApexBatchID__c=BC.getJobId();
                        update MasterContact;
                        system.debug('Apex Batch ID Merge'+MasterContact.ApexBatchID__c);
                        system.debug('Before Merge');
                        merge MasterContact otherContact;
                        system.debug('After Merge');
                        system.debug('Excuted Master Contact'+MasterContact);
                    
                
                }
                
            }
                 else If(lstContact.size()==0){
                    target[0].LastProTime__c=BNow;
                    Update target;
                    system.debug('Duplicate not found'+target);
                
                }
            }
            catch(Exception ex)
            {
                system.debug('Exception'+ex);
            }
            
            
        }
    }
    
    //Finish Method
   }
.........................
 
ShirishaShirisha (Salesforce Developers) 
Hi Bheemudu,

Greetings!

You can use Database.QueryLocator() for execute method and please find the suggestions in the below documents which might help you to achieve your requirement.

https://salesforce.stackexchange.com/questions/57220/test-class-not-covering-the-execute-method

https://salesforce.stackexchange.com/questions/230808/test-class-for-batch-job-how-to-cover-execute-method

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri