0

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 = '[email protected]'];
              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 = '[email protected]'; 
           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  = '[email protected]';
          cwe.CRS_W2L_CC_Email__c  = '[email protected]';
          cwe.CRS_W2L_FROM_Email__c = '[email protected]';
          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 = '[email protected]';
    insert edl; 


  WebtoLeadScheduledProcess testsche = new WebtoLeadScheduledProcess();
       String sch = '0 10 23 * * ?';
       WebtoLeadScheduledProcess.sendMailDistribution();
       system.schedule('Test status Check', sch, testsche ); 

       Test.stopTest();


      }

    }

2 Answers 2

1

First thing is that you don't start your test until after you've created all your data. So move Test.startTest() to after you insert edl; You'd also want to create your runAs user before you start your test too. So try modifying your test to look more like the following:

Also, since you're creating test data, I'm not entirely certain why you're using SeeAllData=true. You presumably should be able to avoid that unless there's something I'm missing. I'd also echo @AnuragA comments as well but am assuming that having declared the Queue, those may be implicit.

@isTest

private class WebtoLeadScheduledProcessTest
{

    static testMethod void doTestWebtoLead(){


          System.runAs(new User(Id=UserInfo.getUserId()))
          {

            Group testGroup = new Group(Name='test group', Type='Queue');
            insert testGroup;

           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 = '[email protected]'; 
           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  = '[email protected]';
          cwe.CRS_W2L_CC_Email__c  = '[email protected]';
          cwe.CRS_W2L_FROM_Email__c = '[email protected]';
          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 = '[email protected]';
    insert edl; 

    Test.startTest();

  WebtoLeadScheduledProcess testsche = new WebtoLeadScheduledProcess();
       String sch = '0 10 23 * * ?';
       WebtoLeadScheduledProcess.sendMailDistribution();
       system.schedule('Test status Check', sch, testsche ); 

     // Verify the scheduled job hasn't run yet.

       Test.stopTest();

         // Query to verify that scheduled job has run & results are as expected.
         // Assert results.

      }
   }
}
2
  • Thank you but still same percentage no more improvement. I am trying from last 2 days... still same percentage....:( Mar 30, 2016 at 15:54
  • Unless you can tell us exactly which lines aren't covered, it's difficult to give you more specific advice. Also, try adding debug statements to your code (not just the test class, but to the schedulable) so you can see what lines are hit and which ones aren't along with why. Your test class also doesn't check to make certain your data got inserted. All those things are important. See How do I start to debug my own Apex code?.
    – crmprogdev
    Mar 30, 2016 at 16:10
0

Did you check the values for , WebToLead_User__c and RecordType.DeveloperName

Are they satisfying the condition in if block below,

l.WebToLead_User__c == 'QUEUE' && l.RecordType.DeveloperName == 'Carrier_Rentals_Lead'
5
  • yes , l.WebToLead_User__c == 'QUEUE' this is formula field. This will check OWNER ID is User or Queue but have written code for queue, you can see in test class and Record type is satisfying. you know one thing if remove this two statements.. even then it's not going in this loop statement. Mar 30, 2016 at 15:53
  • Did you debug and see the formula field value, i suspect that
    – Anurag
    Mar 30, 2016 at 15:57
  • Yes, It's coming the owner id value and if I remove l.WebToLead_User__c == 'QUEUE' && l.RecordType.DeveloperName == 'Carrier_Rentals_Lead' from IF statement still its not going inside the statement. Mar 30, 2016 at 16:02
  • Can you check the lead object record before if condition. Did it have all the values that satisfies condition?
    – Anurag
    Mar 30, 2016 at 16:13
  • debug the webLead and see if it has values.
    – Anurag
    Mar 30, 2016 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .