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
Vijay A 8Vijay A 8 

Please help me test class for code coverage 75%

Hi All,
Below are my Controller and my Test Class. Please help me test class for code coverage 75%.
 
public class ContactDuplicationCon {
    
    private final Contact contact;
    public string accId {get; set;}
    public string accname {get; set;}
    public boolean acclookup {get; set;}
    public boolean accstring {get; set;}
    
    public Datetime activityLastDate;
    public List<ActivityHistory> activityList = new List<ActivityHistory>();
    public List<ActivityHistory> activityListcontact = new List<ActivityHistory>();
    public ContactDuplicationCon(ApexPages.StandardController controller) {
        
        // Define the values for the contact you’re processing based on its ID
        Id id = ApexPages.currentPage().getParameters().get('id');
        accId = ApexPages.currentPage().getParameters().get('accId');        
        //system.debug('accid====='+accId);
        acclookup = true;
        accstring = false;   
        if(accId != NULL){
           Account ac = [select id,name from Account where id =: accId];
            accname = ac.name;
            acclookup = false;
            accstring = true;
        }
        
        this.contact = (id == null) ? new Contact() :
        [SELECT Id, FirstName, LastName, Email, Phone, AccountId ,Sub_Division__c,Account.Sub_Division_Name__c,Address__c,Title,Active__c,Target_Audience__c,MailingStreet,
         MailingCity,MailingState,MailingPostalCode,MailingCountry,    Fax,LeadSource,AssistantName,Department FROM Contact WHERE Id = :id];
             
             }
    // Return contact and its values to the Visualforce page for display
    public Contact getContact() {
        return this.contact;    
    }
    
    public PageReference contactsave() {
        if(accId != Null){
        Contact.AccountId = accId;
        }
        Account acc = [select id,name,Sub_Division_Name__c from account where id =: contact.AccountId];
        List<Contact> cc = new List<Contact>();
        system.debug('account===='+contact.AccountId);
            cc = [Select id,lastname,firstname,email,Sub_Division__c,Account.Sub_Division_Name__c,
                  (Select id,Status,OwnerId,CreatedDate,EndDateTime,LastModifiedDate,WhoId,Priority,Subject From ActivityHistories ORDER BY LastModifiedDate ASC Limit 1)
                  from contact where email =: contact.email AND Sub_Division__c =: acc.Sub_Division_Name__c ];
        
        List<Lead> ld = [Select id,name,email,Sub_Division_Name__c,
                         (Select id,Status,OwnerId,CreatedDate,EndDateTime,LastModifiedDate,WhoId,Priority,Subject From ActivityHistories ORDER BY LastModifiedDate ASC Limit 1)
                         from Lead where email =: contact.email AND Sub_Division_Name__c =: acc.Sub_Division_Name__c];
        //system.debug('subdivision====='+acc.Sub_Division_Name__c);
        //System.debug('Contact List Size =====>'+cc.size());        
        //System.debug('Lead List Size =====>'+ld.size());
        
        //System.debug('email==='+contact.email);
        
        if(cc.size() >= 1){
            for(Contact contacts : cc){
                //System.debug('Inside ContactActivity Loop');
                for(ActivityHistory activityhiscon : contacts.ActivityHistories){
                    activityListcontact.add(activityhiscon);
                }
                System.debug('Contact Activity Size ======>'+activityListcontact.size());
                if(activityListcontact.size() <= 0){
                    //System.debug('Contact Inside Activity List');                    
                    insert contact;
                    PageReference ContactDuplication = new PageReference('/' + Contact.Id);
                    ContactDuplication.setRedirect(true);
                    return ContactDuplication;
                }else{
                    for(ActivityHistory activityhiscon : contacts.ActivityHistories){
                        //System.debug('contact ActivityHistory LastModifiedDate ====> '+activityhiscon.LastModifiedDate);
                        activityLastDate = activityhiscon.LastModifiedDate + 30;
                        //System.debug('Contact activityLastDate  ====> '+activityLastDate);
                        if(activityLastDate > System.now()){
                            User u = [select id,Name,username,email from User where id =: userInfo.getUserId()];
                            Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
                            string [] toAdd = new string[]{'xyz@gmail.com'};
                                mail.setToAddresses(toAdd);
                            mail.setSubject('Contact Duplication');                            
                            mail.setPlainTextBody('Dear Admin'+
                                                  '\n\nBelow User trying to create duplicate email '+contact.email+
                                                  '\n\nName:' + u.Name+
                                                  '\nUserName : '+ u.Username +                                      
                                                  '\n\nThank You.'+
                                                  '\n\n TEAM.');
                            Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {mail};
                                Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
                            if (results[0].success) {
                                System.debug('The email was sent successfully.');
                            } else {
                                System.debug('The email failed to send: '
                                             + results[0].errors[0].message);
                            }
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Email already exist with same subdivision in Contact'));                        }                              
                    }                                              
                }
            }
        }
        else if(ld.size() >= 1){
            System.debug('Inside Lead Loop');
            for(Lead leads : ld){
                //System.debug('Inside LeadActivity Loop');
                for(ActivityHistory activityhis : leads.ActivityHistories){
                    activityList.add(activityhis);
                }
                //System.debug('Activity Size ======>'+activityList.size());
                if(activityList.size() <= 0){
                    System.debug('Lead Inside Activity List');
                    insert contact;
                    PageReference ContactDuplication = new PageReference('/' + Contact.Id);
                    ContactDuplication.setRedirect(true);
                    return ContactDuplication;
                }else{
                    for(ActivityHistory activityhis : leads.ActivityHistories){
                        //System.debug('Lead ActivityHistory LastModifiedDate ====> '+activityhis.LastModifiedDate);
                        activityLastDate = activityhis.LastModifiedDate + 30;
                        //System.debug('Lead activityLastDate  ====> '+activityLastDate);
                        if(activityLastDate > System.now()){
                            User u = [select id,Name,username,email from User where id =: userInfo.getUserId()];
                            Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
                            string [] toAdd = new string[]{'xyz@gmail.com'};
                                mail.setToAddresses(toAdd);
                            mail.setSubject('Lead Duplication');                            
                            mail.setPlainTextBody('Dear Admin'+
                                                  '\n\nBelow User trying to create duplicate email '+contact.email+
                                                  '\n\nName:' + u.Name+
                                                  '\nUserName : '+ u.Username +                                      
                                                  '\n\nThank You.'+
                                                  '\n\nTEAM.');
                            Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {mail};
                                Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
                            if (results[0].success) {
                                System.debug('The email was sent successfully.');
                            } else {
                                System.debug('The email failed to send: '
                                             + results[0].errors[0].message);
                            }
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Email already exist with same subdivision in Lead'));                            
                        }                              
                    }                                              
                }
            }
        }
        
        else{
            insert contact;
            PageReference ContactDuplication = new PageReference('/' + Contact.Id);
            ContactDuplication.setRedirect(true);
            return ContactDuplication;
        }        
        return null;
    }
    
    public PageReference Cancel(){        
        PageReference pageRef = new PageReference('/lightning/o/Contact/list?filterName=Recent');
        pageRef.setRedirect(true);
        return pageRef;          
    }
}

-----------------------------------

@isTest
public class ContactDuplicationConTest {
    static testMethod void testMethod1(){
        lead ld = new lead ();
        ld.LastName = 'Test';
        ld.email = 'testlead@gmail.com';
        ld.Company = 'testcomp';
        ld.Status = 'Followup';
        ld.LeadSource = '360 camp';
        ld.Division_Name__c = 'test division';
        ld.Sub_Division_Name__c = 'test subdivision';        
        insert ld;
        
        Account a = new Account();
        a.Name = 'testaccount';
        a.Sub_Division_Name__c = 'Global division';
        insert a;
        
        contact con = new contact();
        con.LastName = 'testcon';
        con.AccountId = a.id;
        con.Email= 'test@gmail.com';
        insert con;
        
        Event e = new Event();
        e.WhoId=ld.id;
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
        insert e;
        
        Task taskCreate = new Task();
        taskCreate.WhoId = ld.Id;
        taskCreate.Status = 'Completed';
        taskCreate.Priority = 'Normal';
        taskCreate.Subject = 'Dummy subject';
        taskCreate.ActivityDate = system.today();
        insert taskCreate;
        
        apexPages.Currentpage().getParameters().put('Id',con.id);
        Apexpages.StandardController sc = new Apexpages.StandardController(con);
        ContactDuplicationCon ext = new ContactDuplicationCon(sc);
        
        ext.cancel();
        ext.contactsave();
        ext.getContact();
    }
    
    static testMethod void testMethod2(){
        
        lead ld = new lead ();
        ld.LastName = 'Test123';
        ld.Email = 'test1232345575676579@gmail.com';
        ld.Company = 'testcomp123';
        ld.Status = 'Followup';
        ld.LeadSource = '360 camp';
        ld.Division_Name__c = 'test division';
        ld.Sub_Division_Name__c = 'test subdivision';
        insert ld;
        
        Event e = new Event();
        e.WhoId=ld.id;
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
        insert e;
        
        Task taskCreate = new Task();
        taskCreate.WhoId = ld.Id;
        taskCreate.Status = 'Completed';
        taskCreate.Priority = 'Normal';
        taskCreate.Subject = 'Dummy subject';
        taskCreate.ActivityDate = system.today();
        insert taskCreate;
        
        Account a = new Account();
        a.Name = 'testaccount';
        a.Sub_Division_Name__c = 'Global division';
        insert a;        
        
        contact con1 = new contact();
        con1.LastName = 'testcon1';
        con1.AccountId = a.id;
        con1.Email = 'testcon1@gmail.com';        
        insert con1;
        
        apexPages.Currentpage().getParameters().put('Id',con1.id);
        Apexpages.StandardController sc = new Apexpages.StandardController(con1);
        ContactDuplicationCon ext = new ContactDuplicationCon(sc);
        
        ext.cancel();
        ext.contactsave();
        ext.getContact();
        
    }
}

 
Sahil ShitoleSahil Shitole
Hi Vijay,

Instead of:
apexPages.Currentpage().getParameters().put('Id',con.id);
Apexpages.StandardController sc = new Apexpages.StandardController(con);
ContactDuplicationCon ext = new ContactDuplicationCon(sc);

Use:
 PageReference pageReferenceObject = Page.Your_Page_Name;
 pageReferenceObject.getParameters().put('id', con.id);
 pageReferenceObject.getParameters().put('accId', a.id);
 pageReferenceObject.setRedirect(true);
Test.setCurrentPage(pageReferenceObject);
ContactDuplicationCon ext = new ContactDuplicationCon(sc);

Do let me know if this helps you.

Thanks
Vijay A 8Vijay A 8
Hi Sahil,

Thank you for the reply.
I added some more code in class and added 2 more testmethods in test class, Now its covered.
Sahil ShitoleSahil Shitole
Hi Vijay,

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

Thanks