You need to sign in to do that
Don't have an account?
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();
}
}
You need to instantiate some order__c and OrderLine__c objects in order that the majority of your code in sendEmail() gets covered.
Hi ,
I have tried covering the code , Please insert the records inside test class as per your conditions. My Code coverage is 100%.
and this was the class which I used ...
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
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;
Hi ,
Use this to Cover the Schedule Class.
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
It shows error in this line in my Test Class.. What is cover schedule?
Hi ,
Try
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
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;
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