I have inserted all the values, even then it's covering only 40%. From this For loop [for (Lead l : webLead )] and then IF statement is not covering even satisfy those conditions.
global class WebtoLeadScheduledProcess implements Schedulable
{
global void execute(SchedulableContext ctx){
sendMailDistribution();
}
public static void sendMailDistribution(){
CRS_Web2Lead_Emails__c mfauser = [Select Id, Name, CRS_W2L_Subject__c, CRS_W2L_CC_Email__c, CRS_W2L_FROM_Email__c from CRS_Web2Lead_Emails__c limit 1];
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
list<lead> webLead = [Select Name, Is_Web_Lead__c, WebToLead_User__c, Cancel_No_Activity_Workflow__c, RecordType.DeveloperName, leadsource, status, Company, CRS_City__c, CRS_State_Province__C, CRS_Country__c, CRS_Zip__c,
Phone, CRS_Application_Type__c, Comments__c, Email, FirstName, CreatedDate from Lead where CreatedDate = N_DAYS_AGO:9];
Map<String, CRS_W2L_Email_Distribution_List__c> emd = new Map<String, CRS_W2L_Email_Distribution_List__c>();
List<CRS_W2L_Email_Distribution_List__c> listCodes = CRS_W2L_Email_Distribution_List__c.getAll().values();
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'Test@example.com'];
for(CRS_W2L_Email_Distribution_List__c c : listCodes){
emd.put(c.State_Abbreviations__c, c);
}
for (Lead l : webLead ){
if(l.Is_Web_Lead__c == True && l.status == 'open' && l.Cancel_No_Activity_Workflow__c == false && l.leadsource == 'Web Inquiry' && l.WebToLead_User__c == 'QUEUE' && l.RecordType.DeveloperName == 'Carrier_Rentals_Lead') {
CRS_W2L_Email_Distribution_List__c ems = emd.get(l.CRS_State_Province__C);
if(ems != null){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Action required on Unclaimed Lead');
List<String> sendTo = new List<String>();
for(String key : ems.Email_Distribution_List__c.split(','))
{
sendTo.add(key);
}
mail.setToAddresses(sendTo);
mail.setReplyTo(mfauser.CRS_W2L_FROM_Email__c);
mail.setOrgWideEmailAddressId(owea.get(0).Id);
String leadLink = URL.getSalesforceBaseUrl().toExternalForm() +'/' +l.Id;
String ecMsg = '<a style=\'color:1B2BE8\' target="_blank" href="'+leadLink+'">'+leadLink+'</a>';
String body = 'Hello <br><br>';
body += 'Test<br>';
body += 'Test.<br><br><br><br>';
body += 'Link to the Lead: '+ecMsg+ '<br><br>';
body += 'Thank you. <br><br>';
mail.setHtmlBody(body);
mail.useSignature = false;
mails.add(mail);
}
}
}
Messaging.sendEmail(mails);
}
}
Test Class :
@isTest(seeAllData=true)
private class WebtoLeadScheduledProcessTest
{
static testMethod void doTestWebtoLead(){
Test.startTest();
Group testGroup = new Group(Name='test group', Type='Queue');
insert testGroup;
System.runAs(new User(Id=UserInfo.getUserId()))
{
QueuesObject testQueue = new QueueSObject(QueueID = testGroup.id, SObjectType = 'Lead');
insert testQueue;
}
Id leadrecordtypeid = [select id,name from recordtype where SobjectType = 'Lead' AND name = 'Carrier Rentals Lead' limit 1].id;
list<lead> webtolead = new list<lead>();
lead l = new lead(OwnerId = testGroup.Id);
l.Company = 'webtoleadTest';
l.CRS_City__c = 'AK';
l.CRS_State_Province__C = 'AK';
l.CRS_Country__c = 'US';
l.CRS_Zip__c = '7567890';
l.Phone = '835678936';
l.CRS_Application_Type__c = 'Test';
l.Comments__c = 'webtolead';
l.Email = 'test@example.com';
l.FirstName = 'Test';
l.LastName = 'Web';
l.CRS_Address_Line_1__c = 'Test US';
l.Industry = 'TestAK';
l.Is_Web_Lead__c = True;
l.status = 'open';
l.Cancel_No_Activity_Workflow__c = false;
l.leadsource = 'Web Inquiry';
l.CreatedDate = system.today()-5;
l.RecordTypeId = leadrecordtypeid;
webtolead.add(l);
insert webtolead;
// Test.setCreatedDate(l.Id, DateTime.now().addDays(-9));
CRS_Web2Lead_Emails__c cwe = new CRS_Web2Lead_Emails__c();
cwe.Name = 'Test';
cwe.CRS_W2L_Subject__c = 'Test@example.com';
cwe.CRS_W2L_CC_Email__c = 'Test@example.com';
cwe.CRS_W2L_FROM_Email__c = 'Test@example.com';
insert cwe;
CRS_W2L_Email_Distribution_List__c edl = new CRS_W2L_Email_Distribution_List__c ();
edl.Name ='Test';
edl.State_Abbreviations__c = 'Ak';
edl.Email_Distribution_List__c = 'test@example.com';
insert edl;
WebtoLeadScheduledProcess testsche = new WebtoLeadScheduledProcess();
String sch = '0 10 23 * * ?';
WebtoLeadScheduledProcess.sendMailDistribution();
system.schedule('Test status Check', sch, testsche );
Test.stopTest();
}
}