+ Start a Discussion
mohammed Imammohammed Imam 

Hi I have tried test class for the below code but not getting the code coverage,so anyone could please provide test class for this

public class LeadTriggerHandler {
public static void sendEmailforContact(List<Lead> newvalues){
        Set<Id> CampaignId = new Set<Id>();
        SET<ID> LeadId = new Set<ID>();
        for(Lead Leadldetails : newvalues){
            CampaignId.add(Leadldetails.CampaignId__c);
            LeadId.add(Leadldetails.Id);
        }
        if(!CampaignId.isEmpty()){
            Lead newlead =new Lead();
            List<String>  LeadfieldNames = new List<String>{'Id','Email','FirstName','LastName'};
                if(SecurityUtil.checkAccessibility('Lead',LeadfieldNames)){
                    system.debug('LeadId--->'+LeadId);
                    newlead = [SELECT Id, LastName, FirstName, Email FROM Lead WHERE ID In: LeadId];
                }
            else{
                throw new System.AssertException('Error: Insufficient Access');
            }
            Campaign campaignList = new  Campaign();
            List<String>  CampaignfieldNames = new List<String>{'Id','Name','Start_Date_Time__c','Contact__c','End_Date_Time__c','Online_Event_URL__c',
                'Address1__c','State__c','Event_Location__c', 'City__c','Country__c', 'Postal_Code__c',
                'Logo_Url__c', 'Banner_Url__c'}
                    if(SecurityUtil.checkAccessibility('Campaign',CampaignfieldNames)){
                        campaignList = [SELECT Id, Name,tart_Date_Time__c,Address1__c,City__c,Contact__c,State__c,Contact__r.Name,Contact__r.Email,
                                        Country__c,Postal_Code__c,End_Date_Time__c,Online_Event_URL__c,Event_Location__c,Logo_Url__c,Banner_Url__c From Campaign WHERE Id =: CampaignId WITH SECURITY_ENFORCED];     
                    }
            else{
                throw new System.AssertException('Error: Insufficient Access');
            }
            
            
            Set<Id> contactId =  new Set<Id>();
            EmailTemplate emailTemplateforContact = [select Id, Subject, HtmlValue, Body from EmailTemplate WHERE Name =: System.label.Assist_Mail_Alert_for_Contact];
            String subjectforContact = emailTemplateforContact.Subject;
            String htmlbodyforcontact = emailTemplateforContact.HtmlValue;
            String plainBodyforContact = emailTemplateforContact.Body;
            //HTML value for contact
            
            htmlbodyforcontact = htmlbodyforcontact.replace('{!Contact.Name}', campaignList.Contact__r.Name);
            htmlbodyforcontact = htmlbodyforcontact.replace('{!Campaign.Name__c}', campaignList.Name);
            htmlbodyforcontact = htmlbodyforcontact.replace('{!Lead.FirstName}', newlead.FirstName );
            htmlbodyforcontact = htmlbodyforcontact.replace('{!Lead.LastName}',  newlead.LastName);
            htmlbodyforcontact = htmlbodyforcontact.replace('{!Lead.Email}',  newlead.Email);
            htmlbodyforcontact = htmlbodyforcontact.replace('{!Campaign.Logo_Url__c}', campaignList.Logo_Url__c);
            //PlainBody for contact
            plainBodyforContact = plainBodyforContact.replace('{!Campaign.Name__c}', campaignList.Name);
            plainBodyforContact = plainBodyforContact.replace('{!Lead.FirstName}', newlead.FirstName );
            plainBodyforContact = plainBodyforContact.replace('{!Lead.LastName}',  newlead.LastName);
            plainBodyforContact = plainBodyforContact.replace('{!Lead.Email}',  newlead.Email);
            plainBodyforContact = plainBodyforContact.replace('{!Campaign.Logo_Url__c}', campaignList.Logo_Url__c);
            //Mail for contact
            Messaging.Singleemailmessage mailforcontact = new Messaging.Singleemailmessage();
            mailforcontact.setReplyTo(campaignList.Contact__r.Email);
            mailforcontact.setSenderDisplayName(campaignList.Contact__r.Email);
            mailforcontact.setTargetObjectId(campaignList.Contact__c );
            mailforcontact.setSaveAsActivity(true);
            mailforcontact.setSubject(subjectforContact);
            mailforcontact.setHtmlBody(htmlbodyforcontact);
            mailforcontact.setPlainTextBody(plainBodyforContact);
            Messaging.sendEmail(new Messaging.SingleEmailmessage[] {mailforcontact});
        }
    }
}
 
 
Best Answer chosen by mohammed Imam
CharuDuttCharuDutt
Hii Mohammed
Try Below Test Class
@isTest 
 public Class LeadTriggerHandlerTest { 
 @isTest
 public Static Void unitTest(){

Contact Con = new Contact();
Con.FirstName = 'Test';
Con.LastName = 'Contact';
Con.Email = 'Test1234@test.com';
Insert Con; 

lead l = new Lead();
l.FirstName = 'test';
l.LastName = 'Lead';
l.Company = 'test Company';
l.LeadStatus = 'Open - Not Contacted';
l.Email = 'Test1234@test.com';
insert l;

Campaign c = new Campaign();
        c.Name = 'Test Campaign';
		c.LeadId = l.Id;
		c.tart_Date_Time__c = /*FIll This*/;
		c.Address1__c = /*FIll This*/;
		c.City__c = /*FIll This*/;
		c.Contact__c = /*FIll This*/;
		c.State__c = /*FIll This*/;
		c.Contact__r.Name = Con.Name;
		c.Contact__r.Email = Con.Email;
		c.Country__c = /*FIll This*/;
		c.Postal_Code__c = /*FIll This*/;
		c.End_Date_Time__c = /*FIll This*/;
		c.Online_Event_URL__c = /*FIll This*/;
		c.Event_Location__c = /*FIll This*/;
		c.Logo_Url__c = /*FIll This*/;
		c.Banner_Url__c = /*FIll This*/;
        insert c;
l.CampaignId__c = c.Id;
update l;
list<lead> lstlead = new list<Lead>{l};
	LeadTriggerHandler.sendEmailforContact(lstlead);
 }
 }
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Mohammed
Try Below Test Class
@isTest 
 public Class LeadTriggerHandlerTest { 
 @isTest
 public Static Void unitTest(){

Contact Con = new Contact();
Con.FirstName = 'Test';
Con.LastName = 'Contact';
Con.Email = 'Test1234@test.com';
Insert Con; 

lead l = new Lead();
l.FirstName = 'test';
l.LastName = 'Lead';
l.Company = 'test Company';
l.LeadStatus = 'Open - Not Contacted';
l.Email = 'Test1234@test.com';
insert l;

Campaign c = new Campaign();
        c.Name = 'Test Campaign';
		c.LeadId = l.Id;
		c.tart_Date_Time__c = /*FIll This*/;
		c.Address1__c = /*FIll This*/;
		c.City__c = /*FIll This*/;
		c.Contact__c = /*FIll This*/;
		c.State__c = /*FIll This*/;
		c.Contact__r.Name = Con.Name;
		c.Contact__r.Email = Con.Email;
		c.Country__c = /*FIll This*/;
		c.Postal_Code__c = /*FIll This*/;
		c.End_Date_Time__c = /*FIll This*/;
		c.Online_Event_URL__c = /*FIll This*/;
		c.Event_Location__c = /*FIll This*/;
		c.Logo_Url__c = /*FIll This*/;
		c.Banner_Url__c = /*FIll This*/;
        insert c;
l.CampaignId__c = c.Id;
update l;
list<lead> lstlead = new list<Lead>{l};
	LeadTriggerHandler.sendEmailforContact(lstlead);
 }
 }
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
CharuDuttCharuDutt
Hii Mohammed
Please Close your Query By Marking It As Best Answer  If It Helps So It Also Helps Others In Future