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
Matt110Matt110 

please help me to cover this code coverage

thanks in advance,. am new to sfdc dev.. please help me asap. my current code coverage is 34%

 

global class ShippedDailyEmail implements Schedulable {

List<OrderLine__c> olList = new List<OrderLine__c>();
list<order__c>orderlist=new list<order__c>();
set<id>orderid= new set<id>();
set<id> orderset=new set<id>();
global void execute (SchedulableContext SC) {
    sendEmail();
}

public void sendEmail() {
    List<Messaging.SingleEmailMessage>  myEmails = new List<Messaging.SingleEmailMessage>();
    dateTime todaysDate = system.now();   
    dateTime yesterdaysDate = todaysDate.addDays(-1);
    //system.debug('yesterday*****'+yesterdaysDate );
    system.debug('today*********'+todaysDate);
    //datetime yesterdaysDate = todaysDate.addMinutes(-5);
    system.debug('yesterday*****'+yesterdaysDate );  
   
     for (orderline__c ol: [select Name, SerialNo__c, order__r.id , order__r.requestor_email__c, order__r.requestor__c from orderline__c
                                where Shipped_Email_Sent__c = False and Actual_Ship_DateTime__c > :yesterdaysDate
                                and Actual_Ship_DateTime__c <:todaysDate])
        { 
            //add the orderline to the List
            olList.add(ol);
            orderid.add(ol.order__c);
            System.debug('Order with Shipment: ' + olList);
        }  
    
    System.debug('Order List: ' + olList.size());
    //query the list of orders and related details given the orderline list
   
   // List<orderline__c> orderList = new List<orderline__c>();
   /*
     if (olList.size()>0) {
        System.debug('Order List has records: ' + olList.size());
        orderList = [select order__r.id , order__r.requestor_email__c, order__r.requestor__c 
                     from orderline__c  
                     where Name in :olList];
    } else {
        System.debug('Order List is null');
        orderList = null;
    }  
    */
    orderlist=[select id,requestor__c from order__c where id in :orderid];                                    
    if (orderlist!= null) {
        for (order__c o: orderlist) {
        myEmails.clear();
        system.debug('count***********'+o.id);
                //build the email for each Order
               
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                // select the template
                mail.setTemplateId(System.label.DailyShippedEmailTemplate);
               
                // set the object ID the email is about
                mail.setwhatid(o.id);
                System.debug('Order id =' + o.id);
               
                // set the receipient to the correct contact
                mail.setTargetObjectId(o.requestor__c);
                //mail.setTargetObjectId('003V000000B0XIh');
               
                // set the reply-to address
                //mail.setReplyTo(System.label.ReplyToAddress);
                mail.setOrgWideEmailAddressId(System.label.ReplyToAddressID);
                //mail.setSenderDisplayName(System.label.SenderDisplayName);
                           
                // populate the CCList from the custom label
                    
                String CCList = System.label.EmailCCList;
                String[] toCC = new String[] {};
                toCC = CCList.split(',');
                mail.setCCAddresses(toCC);
              
                mail.SaveAsActivity = false;
                myEmails.add(mail);        
     
           
            try {      
                // send the emails
                Messaging.sendEmail(myEmails);
                orderset.add(o.id);
                      } catch (DMLException e) {
                System.debug('Error sending email - Error: ' + e.getDMLMessage(0));
                return;
            } catch (exception e1) {
                System.debug('Error sending email - Error: ' + e1.getMessage());
                return;
            }  
        }
       for(orderline__c o :[select id from orderline__c where order__c in:orderset and Shipped_Email_Sent__c = False and Actual_Ship_DateTime__c > :yesterdaysDate
                                and Actual_Ship_DateTime__c <:todaysDate]) 
       {
             o.Shipped_Email_Sent__c = True;
             update o;          
       }
    }
    }

  @IsTest(SeeAllData=true) public static void testShippedDailyEmail() {
           
    ShippedDailyEmail controller = new ShippedDailyEmail();
    controller.sendEmail();
   
  }
}

 

 

Boman@imtBoman@imt

You need to instantiate some order__c and OrderLine__c objects in order that the majority of your code in sendEmail() gets covered.

 

gautam_singhgautam_singh

Hi ,

I have tried covering the code , Please insert the records inside test class as per your conditions. My Code coverage is 100%.

@IsTest(SeeAllData=true) 
public class Test_ShippedDailyEmail
{
    public static testmethod void myTestMethod()
    {
      Test.startTest();
      gautamsingh__Order__c OL = new gautamsingh__Order__c();
      OL.Name='test';
      Insert OL;
      
      gautamsingh__Order_List__c OLP = new gautamsingh__Order_List__c();
      OLP.Name='test';
      Insert OLP;
      
      ShippedDailyEmail SDE = new ShippedDailyEmail();
      String schedule = '0 0 21 * * ?';
      system.schedule('Cover Schedule', schedule, SDE);
      
      
    
          
      Test.stopTest();    
    }
}

 and this was the class which I used ...

global class ShippedDailyEmail implements Schedulable {
    List<gautamsingh__Order_List__c> olList = new List<gautamsingh__Order_List__c>();
    List<gautamsingh__Order_List__c> orderlist= new list<gautamsingh__Order_List__c>();
    
    public List<gautamsingh__Order_List__c> updateList {get;set;}
    
    Set<id> orderid= new set<id>();
    Set<id> orderset=new set<id>();
    
    
    
    global void execute (SchedulableContext SC) {
        updateList = new  List<gautamsingh__Order_List__c>();
        sendEmail();
    }

    public void sendEmail() {
        List<Messaging.SingleEmailMessage>  myEmails = new List<Messaging.SingleEmailMessage>();
        dateTime todaysDate = system.now();   
        dateTime yesterdaysDate = todaysDate.addDays(-1);
        
        for (gautamsingh__Order_List__c ol: [select Id , Name from gautamsingh__Order_List__c ]){ 
            olList.add(ol);
            orderid.add(ol.id);
        }  
        
        system.debug('## olList' + olList + olList.size() );
        system.debug('### orderid' + orderid + + orderid.size() );
    
        orderlist = [select id,Name from gautamsingh__Order_List__c where id in :orderid];  
        system.debug('### orderid' + orderid + orderid.size() );                                  
        if (orderlist.size() > 0) {
            for (gautamsingh__Order_List__c o : orderlist) {
                myEmails.clear();
                
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
               // mail.setwhatid(o.id);
                //mail.setTargetObjectId(o.gautamsingh__Requestor__c);
                String CCList = 'gautam.salesforce@gmail.com';
                String[] toCC = new String[] {};
                toCC = CCList.split(',');
                mail.setCCAddresses(toCC);
                mail.SaveAsActivity = false;
                myEmails.add(mail);        
                
                try {      
                    Messaging.sendEmail(myEmails);
                    
                    }catch (exception e1) {
                    return;
                    }  
                    //orderset.add(o.id);
            }
            
            
           
            
            
          
        
        
        }
        
    }

     
}

 

Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

Matt110Matt110

Gautam Hi,

Thanks for your support. now I covered 70% of my code.. Here am unable to cover these lines in my controller,

controller and my test class are as follows.

 

 

 

Controller:

 

global void execute (SchedulableContext SC) {
sendEmail();

 

 

 

for(orderline__c o :[select id from orderline__c where order__c in:orderset and Shipped_Email_Sent__c = False and Ship_DateTime__c > :yesterdaysDate
                                and Ship_DateTime__c <:todaysDate]) 
       {
             o.Shipped_Email_Sent__c = True;
             update o;          
       }


Test Class:
    
    order__c order = new order__c();
        order.name='test'
     insert order;
   
    orderline__c ol=new orderline__c();
            
            ol.SerialNo__c = 'ABc';
            ol.Shipped_Email_Sent__c = false;
            ol.PartNumber__c=pdt.id;
            ol.Ship_DateTime__c = system.now();
            ol.Order__c=order.id;
            
           dateTime todaysDate = system.now();            
            ol.Ship_DateTime__c = todaysDate.addHours(-1) ;
            insert ol;  

gautam_singhgautam_singh

Hi ,

Use this to Cover the Schedule Class.

ShippedDailyEmail SDE = new ShippedDailyEmail();
      String schedule = '0 0 21 * * ?';
      system.schedule('Cover Schedule', schedule, SDE);

 Also,for the next string you are writing an update in for loop which willl run into exception. Please create a list and update that outside for loop. Also when you use a list make sure that first you check the list size so that you dont run in null errors.

One more thing which i would like to quote is check by using system.debug whether values are coming inside orderlist as this is due to which error is coming. Please let me know if it worked or not.




Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

Matt110Matt110
system.schedule('Cover Schedule', schedule, SDE);

It shows error in this line in my Test Class.. What is cover schedule?
gautam_singhgautam_singh

Hi , 

Try


String schedule = '0 0 21 * * ?';
system.schedule('MySchedule', schedule, SDE); 

   where


    MySchedule is JobName
    Schedule is the variable defined above.
    SDE is Object Instance.


Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

Matt110Matt110

Thanks Gautam,

 

It covered the schedulable line.. Can you help me to cover the following line

 

Check the highlighted lines are correct.. Thankx muxh gautam for your contribution

 

 

for(orderline__c o :[select id from orderline__c where order__c in:orderset and Shipped_Email_Sent__c = False and Ship_DateTime__c > :yesterdaysDate
                                and Ship_DateTime__c <:todaysDate]) 
       {
             o.Shipped_Email_Sent__c = True;
             update o;          
       }

 

Test Class:

 

orderline__c ol=new orderline__c();
            
            ol.SerialNo__c = 'ABc';
            ol.Shipped_Email_Sent__c = false;
            ol.PartNumber__c=pdt.id;
            ol.Ship_DateTime__c = system.now();
            ol.Order__c=order.id;
             
           dateTime todaysDate = system.now();            
            ol.Ship_DateTime__c = todaysDate.addHours(-1) ;
            insert ol;

gautam_singhgautam_singh

Hi,

1. In the exceprt of code, you are doing a bad practice. We should not update inside a for loop which you are doing. Please change the concept first and then it will be easily covered.

2. Try to make it like you collect all of the OrderLine in a list first in the for loop and then update the list outside the for loop.

3. Check values are coming inside orderset or not.

Change the functionality of the code for this for loop. You can refer this for more details : Click Here


Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You