You need to sign in to do that
Don't have an account?

Scheduler test class code coverage
Hi Friends,
I had done with my scheduler and test class but I got stuck in code coverage issue and could not able to deploy into production.
Kindly need your help to complete the code.
Class :
global class orderItemsSchedulable Implements Schedulable {
global void execute(SchedulableContext sc) {
insertOrder();
}
public void insertOrder() {
List<OrderItems__c> orderitemsList = new List<OrderItems__c>();
orderitemsList = [SELECT Id, Name, Item_Id__c, Due_Date__c, Name__c, Discount_Amount__c, Price__c, Product_Id__c, Product_Type__c, Qty_Backordered__c,
Qty_Canceled__c, Qty_Invoiced__c, Qty_Ordered__c, Qty_Refunded__c, Qty_Returned__c, Qty_Shipped__c, Sku__c
FROM OrderItems__c WHERE CreatedDate = TODAY AND HOUR_IN_DAY(CreatedDate) > 1];
System.debug('--- Order Items List Created ---' + orderitemsList.size());
Set<String> orderIds = new Set<String>();
List<Opportunitylineitem> oliinsertList = new List<Opportunitylineitem>();
for(OrderItems__c oi : orderitemsList) {
orderIds.add(oi.Name);
orderIds.add(oi.Name__c);
System.debug('--- OrderId Name: ---' + oi.Name);
System.debug('--- OrderId Name: ---' + oi.Name__c);
}
Map<String, List<Opportunitylineitem>> oliMap = new Map<String, List<Opportunitylineitem>>();
Map<String, List<Opportunity>> oppMap = new Map<String, List<Opportunity>>();
List<Opportunity> oppsList = new List<Opportunity>();
oppsList = [SELECT Id, Name, (SELECT Id FROM Opportunitylineitems) FROM Opportunity WHERE Name IN: orderIds];
if(!orderIds.isEmpty()) {
for(Opportunity opps : oppsList) {
if(!oppMap.containsKey(opps.Name)){
oppMap.put(opps.Name, new List<Opportunity> {opps});
System.debug('--- Inside OppMap ---' + oppMap);
}
else{
List<Opportunity> oppList = oppMap.get(opps.Name);
oppList.add(opps);
oppMap.put(opps.Name, oppList);
System.debug('--- Else oppMap ---' + oppList);
}
}
}
Pricebookentry pbe = [SELECT Id, Name, isActive FROM Pricebookentry WHERE isActive = true AND Name = 'Sample Product' Limit 1];
for(OrderItems__c oi : orderitemsList) {
if(oppMap.containsKey(oi.Name)) {
for(Opportunity opp : oppMap.get(oi.Name)) {
if(opp.opportunitylineitems.size() == 0) {
Opportunitylineitem oli = new Opportunitylineitem ();
oli.OpportunityId = opp.Id;
oli.PricebookEntryId = pbe.Id;
oli.Quantity = 1;
oli.TotalPrice = oi.Price__c;
oli.item_id__c = oi.item_id__c;
oli.Name__c = oi.Name__c;
oli.product_id__c = oi.product_id__c;
oli.Due_Date__c = oi.Due_Date__c;
oli.product_type__c = oi.product_type__c;
oli.qty_backordered__c = oi.qty_backordered__c;
oli.qty_canceled__c = oi.qty_canceled__c;
oli.qty_invoiced__c = oi.qty_invoiced__c;
oli.qty_ordered__c = oi.qty_ordered__c;
oli.qty_refunded__c = oi.qty_refunded__c;
oli.qty_returned__c = oi.qty_returned__c;
oli.qty_shipped__c = oi.qty_shipped__c;
oli.Discount_Amount__c = oi.Discount_Amount__c;
oli.Sku__c = oi.Sku__c;
oliinsertList.add(oli);
}
}
}
}
if(oliinsertList.size() > 0) {
insert oliinsertList;
}
}
}
test class :
@isTest(SeeAllData=true)
public class orderItemsSchedulableTest {
Static testmethod void schedulerTest() {
String CRON_EXP = '0 05 * * * ?';
Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
Account acc = new Account(LastName = 'Test Account', recordtypeid = RecordTypeIdAccount);
insert acc;
Id pricebookId = Test.getStandardPricebookId();
List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];
//Create your product
Product2 prod = new Product2(
Name = 'Product X',
ProductCode = 'Pro-X',
isActive = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
Opportunity opp = [SELECT Id, Name FROM Opportunity WHERE Name ='Sample'];
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = opp.Id;
oli.PricebookEntryId = pbEntry.Id;
oli.Quantity = 1;
oli.TotalPrice = 100;
oli.item_id__c = 123;
oli.Name__c = 'Testing';
oli.product_id__c = '1254';
oli.Due_Date__c = System.today();
oli.product_type__c = '32675';
oli.qty_backordered__c = 0;
oli.qty_canceled__c = 0;
oli.qty_invoiced__c = 2;
oli.qty_ordered__c = 1;
oli.qty_refunded__c = 0;
oli.qty_returned__c = 0;
oli.qty_shipped__c = 2;
oli.Sku__c = 'BUYCDF786';
insert oli;
Test.startTest();
String jobId = system.schedule('ScheduleJobTest', CRON_EXP, new orderItemsSchedulable());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id=: jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
}
Thanks.
I had done with my scheduler and test class but I got stuck in code coverage issue and could not able to deploy into production.
Kindly need your help to complete the code.
Class :
global class orderItemsSchedulable Implements Schedulable {
global void execute(SchedulableContext sc) {
insertOrder();
}
public void insertOrder() {
List<OrderItems__c> orderitemsList = new List<OrderItems__c>();
orderitemsList = [SELECT Id, Name, Item_Id__c, Due_Date__c, Name__c, Discount_Amount__c, Price__c, Product_Id__c, Product_Type__c, Qty_Backordered__c,
Qty_Canceled__c, Qty_Invoiced__c, Qty_Ordered__c, Qty_Refunded__c, Qty_Returned__c, Qty_Shipped__c, Sku__c
FROM OrderItems__c WHERE CreatedDate = TODAY AND HOUR_IN_DAY(CreatedDate) > 1];
System.debug('--- Order Items List Created ---' + orderitemsList.size());
Set<String> orderIds = new Set<String>();
List<Opportunitylineitem> oliinsertList = new List<Opportunitylineitem>();
for(OrderItems__c oi : orderitemsList) {
orderIds.add(oi.Name);
orderIds.add(oi.Name__c);
System.debug('--- OrderId Name: ---' + oi.Name);
System.debug('--- OrderId Name: ---' + oi.Name__c);
}
Map<String, List<Opportunitylineitem>> oliMap = new Map<String, List<Opportunitylineitem>>();
Map<String, List<Opportunity>> oppMap = new Map<String, List<Opportunity>>();
List<Opportunity> oppsList = new List<Opportunity>();
oppsList = [SELECT Id, Name, (SELECT Id FROM Opportunitylineitems) FROM Opportunity WHERE Name IN: orderIds];
if(!orderIds.isEmpty()) {
for(Opportunity opps : oppsList) {
if(!oppMap.containsKey(opps.Name)){
oppMap.put(opps.Name, new List<Opportunity> {opps});
System.debug('--- Inside OppMap ---' + oppMap);
}
else{
List<Opportunity> oppList = oppMap.get(opps.Name);
oppList.add(opps);
oppMap.put(opps.Name, oppList);
System.debug('--- Else oppMap ---' + oppList);
}
}
}
Pricebookentry pbe = [SELECT Id, Name, isActive FROM Pricebookentry WHERE isActive = true AND Name = 'Sample Product' Limit 1];
for(OrderItems__c oi : orderitemsList) {
if(oppMap.containsKey(oi.Name)) {
for(Opportunity opp : oppMap.get(oi.Name)) {
if(opp.opportunitylineitems.size() == 0) {
Opportunitylineitem oli = new Opportunitylineitem ();
oli.OpportunityId = opp.Id;
oli.PricebookEntryId = pbe.Id;
oli.Quantity = 1;
oli.TotalPrice = oi.Price__c;
oli.item_id__c = oi.item_id__c;
oli.Name__c = oi.Name__c;
oli.product_id__c = oi.product_id__c;
oli.Due_Date__c = oi.Due_Date__c;
oli.product_type__c = oi.product_type__c;
oli.qty_backordered__c = oi.qty_backordered__c;
oli.qty_canceled__c = oi.qty_canceled__c;
oli.qty_invoiced__c = oi.qty_invoiced__c;
oli.qty_ordered__c = oi.qty_ordered__c;
oli.qty_refunded__c = oi.qty_refunded__c;
oli.qty_returned__c = oi.qty_returned__c;
oli.qty_shipped__c = oi.qty_shipped__c;
oli.Discount_Amount__c = oi.Discount_Amount__c;
oli.Sku__c = oi.Sku__c;
oliinsertList.add(oli);
}
}
}
}
if(oliinsertList.size() > 0) {
insert oliinsertList;
}
}
}
test class :
@isTest(SeeAllData=true)
public class orderItemsSchedulableTest {
Static testmethod void schedulerTest() {
String CRON_EXP = '0 05 * * * ?';
Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
Account acc = new Account(LastName = 'Test Account', recordtypeid = RecordTypeIdAccount);
insert acc;
Id pricebookId = Test.getStandardPricebookId();
List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];
//Create your product
Product2 prod = new Product2(
Name = 'Product X',
ProductCode = 'Pro-X',
isActive = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
Opportunity opp = [SELECT Id, Name FROM Opportunity WHERE Name ='Sample'];
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = opp.Id;
oli.PricebookEntryId = pbEntry.Id;
oli.Quantity = 1;
oli.TotalPrice = 100;
oli.item_id__c = 123;
oli.Name__c = 'Testing';
oli.product_id__c = '1254';
oli.Due_Date__c = System.today();
oli.product_type__c = '32675';
oli.qty_backordered__c = 0;
oli.qty_canceled__c = 0;
oli.qty_invoiced__c = 2;
oli.qty_ordered__c = 1;
oli.qty_refunded__c = 0;
oli.qty_returned__c = 0;
oli.qty_shipped__c = 2;
oli.Sku__c = 'BUYCDF786';
insert oli;
Test.startTest();
String jobId = system.schedule('ScheduleJobTest', CRON_EXP, new orderItemsSchedulable());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id=: jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
}
Thanks.
Thanks for your reply.
insertOrder obj = new insertOrder(); // Am getting error on this line.
Invalid type: insertOrder
Let me know how to resolve and what is this line.
Please insert the data whihc is needed to run your scheduler Class:
You need to insert the Opportunity and OrderItems__c to run the Scheduler:
Please pass the proper data in the below class and run the Class.
@isTest(SeeAllData=true)
public class orderItemsSchedulableTest {
Static testmethod void schedulerTest() {
// Insert Account with All the Required Fields
Account acc = new Account(LastName = 'Test Account');
insert acc;
//Insert Opportunity with All the Required Fields
Opportunity opp = new Account(Name = 'Sample',AccountId = acc.Id,StageName = 'Prospecting',CloseDate = date.today());
insert opp;
//Insert Order Item with All the Required Fields
//Insert if any other related or Parent record is needed
OrderItems__c orditem = new OrderItems__c();
orditem.orderitems.Name = 'Sample';
orditem.Due_Date__c = date.today();
orditem.Discount_Amount__c = 10000;
orditem.Price__c = 500;
orditem.Qty_Invoiced__c = True;
orditem.Qty_Ordered__c = True;
// Add all the remaining Fields with your proper Inputs for creating the OrderItems__c Record.
insert orditem;
Test.startTest();
orderItemsSchedulable sh1 = new orderItemsSchedulable();
String sch = '0 05 * * * ?';
system.schedule('Test orderItemsSchedulable', sch, sh1);
Test.stopTest();
}
}
If this helps to solve your query, do mark this as Best Answer!!!.
Thanks
Maharajan.C
It is not inserting the opportunity line item record. My codecoverage is 56%. Kindly refer the below screenshot.
Thanks.