I have written the below apex class
public with sharing class BillingMonthlyCustomApp {
public BillingMonthlyCustomApp() {}
public static void insertRecordsIntoSchedule(Set<Id> orderLineItemIds) {
System.debug('The order line items that have come from trigger are as follows: ' + orderLineItemIds);
// Fetch all OrderItems in one SOQL query
List<OrderItem> orderItemList = [SELECT Id, OrderId, Order.Billing_Type__c, Line_Item_End_Date__c, Line_Item_Start_Date__c, Delivery_Contact_Email__c, Purchase_Order__c, OL_Left_To_Deliver__c, Total_Price__c, Pacing__c, Quantity, Pacing_Notes__c, UnitPrice FROM OrderItem WHERE Id IN :orderLineItemIds];
System.debug('The class has been entered Akshay 1' + orderItemList);
List<Schedule__c> relatedRecords = new List<Schedule__c>();
List<OrderItem> recordsToProcess = new List<OrderItem>();
List<Id> orderItemIds = new List<Id>();
try {
// Collect OrderItem IDs
for (OrderItem oli : orderItemList) {
orderItemIds.add(oli.Id);
}
if (!orderItemIds.isEmpty()) {
// Query related Schedule__c records in one SOQL query
relatedRecords = [SELECT Id FROM Schedule__c WHERE Schedule_Status__c = 'Live' AND Schedule_Type__c = 'Monthly Customer Approval Billing Schedule' AND Order_Product__c IN :orderItemIds];
delete relatedRecords;
}
// Process OrderItems
for (OrderItem oli : orderItemList) {
recordsToProcess.add(oli);
System.debug('Akshay 0' + recordsToProcess);
if (recordsToProcess.size() == 200) {
processScheduleRecords(recordsToProcess);
recordsToProcess.clear();
}
}
// Insert any remaining records
if (!recordsToProcess.isEmpty()) {
processScheduleRecords(recordsToProcess);
}
} catch (Exception e) {
System.debug('An error occurred while updating the line item records: ' + e.getMessage() + ' line number ' + e.getLineNumber());
}
}
public static void processScheduleRecords(List<OrderItem> processScheduleRecords) {
List<Schedule__c> scheduleRecordList = new List<Schedule__c>();
List<OrderItem> oliRecordsToUpdate = new List<OrderItem>();
List<Id> updatableOrderId = new List<Id>();
Date startDate;
Date endDate;
System.debug('Akshay 4 just entered the second method');
try {
for (OrderItem oli : processScheduleRecords) {
updatableOrderId.add(oli.Id);
if (System.today().daysBetween(oli.Line_Item_Start_Date__c) > 0) {
startDate = oli.Line_Item_Start_Date__c;
} else {
startDate = System.today();
}
endDate = oli.Line_Item_End_Date__c;
Integer monthDiff = startDate.monthsBetween(endDate);
System.debug('The month of difference is Akshay 23 ' + monthDiff);
if (monthDiff == 0) {
if (Test.isRunningTest()) {
System.debug('This test condition is working ak25 ' + oli.Order.Billing_Type__c);
}
if (oli.Order.Billing_Type__c == 'Monthly Customer Approval') {
if (Test.isRunningTest()) {
System.debug('This test condition is working ak26 ' + oli.Order.Billing_Type__c);
}
Schedule__c sc = new Schedule__c();
sc.Schedule_Date__c = startDate.toStartOfMonth();
sc.Schedule_Status__c = 'Live';
sc.Schedule_Type__c = 'Monthly Customer Approval Billing Schedule';
sc.Order_Product__c = oli.Id;
sc.Order__c = oli.OrderId;
sc.Unit_Price__c = 0;
sc.Quantity__c = 1;
scheduleRecordList.add(sc);
}
} else if (monthDiff >= 1) {
System.debug('The monthly customer approval entered more than one loop Akshay 26');
if (Test.isRunningTest()) {
System.debug('This test condition is working ak28 ' + oli.Order.Billing_Type__c);
}
if (oli.Order.Billing_Type__c == 'Monthly Customer Approval') {
System.debug('The billing type entered more than one loop Akshay 27');
if (Test.isRunningTest()) {
System.debug('This test condition is working ak27 ' + oli.Order.Billing_Type__c);
}
for (Integer i = 0; i <= monthDiff; i++) {
Schedule__c sc = new Schedule__c();
sc.Schedule_Date__c = startDate.addMonths(i).toStartOfMonth();
sc.Schedule_Status__c = 'Live';
sc.Schedule_Type__c = 'Monthly Customer Approval Billing Schedule';
sc.Order_Product__c = oli.Id;
sc.Order__c = oli.OrderId;
sc.Unit_Price__c = 0;
sc.Quantity__c = 1;
scheduleRecordList.add(sc);
}
}
} else {
oli.addError('You can't run this automation as the End Date for this campaign has already been crossed. Please update the End Date to create Schedule Records.');
}
System.debug('The list of Scheduled records Akshay 5 ' + scheduleRecordList);
}
if (!scheduleRecordList.isEmpty()) {
insert scheduleRecordList;
}
if (!updatableOrderId.isEmpty()) {
updateRecordsAsync(updatableOrderId);
}
} catch (Exception e) {
System.debug('An error occurred while updating the line item records: ' + e.getMessage() + ' line number ' + e.getLineNumber());
}
}
@future
public static void updateRecordsAsync(List<Id> recordIds) {
System.debug('We are working with the Monthly schedule records here Akshay 6' + recordIds);
List<OrderItem> recordsToUpdate = [SELECT Id, Schedule_Automation_Status__c FROM OrderItem WHERE Id IN :recordIds];
for (OrderItem record : recordsToUpdate) {
record.Schedule_Automation_Status__c = 'Ready';
}
update recordsToUpdate;
}
}
and for that I have written the below test class.
@isTest
public with sharing class BillingMonthlyCustomAppTest {
@isTest
static void BillingScheduleTest() {
List<Account> updateAccount = new List<Account>();
List<User> updateUser = new List<User>();
List<order> updateOrder = new List<Order>();
List<OrderItem> updateOrderLineItem = new List<OrderItem>();
String accountDetailsFuture;
String userDetailsFuture;
String orderDetailsFuture;
String orderItemDetailsFuture;
String soName;
String amName;
String prepaid;
String orderFileLocation;
//String priceBookDefaultId = returnPriceBookId();
List<Order> newOrdList = new List<Order>();
User so = new User();
so.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id;
//so.RoleId = [SELECT Id FROM Role WHERE Name = 'Sales Team East'].Id;
so.FirstName = 'SO';
so.LastName = 'user';
so.Email = '[email protected]';
so.Special_Permissions__c = 'Sales Owner';
so.Username = '[email protected]' + System.currentTimeMillis();
so.CompanyName = 'TEST';
so.Title = 'title';
so.Alias = 'alias';
so.FederationIdentifier = '[email protected]';
so.TimeZoneSidKey = 'America/Los_Angeles';
so.EmailEncodingKey = 'UTF-8';
so.LanguageLocaleKey = 'en_US';
so.LocaleSidKey = 'en_US';
insert so;
User am = new User();
am.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id;
am.FirstName = 'AM';
am.LastName = 'user';
am.Email = '[email protected]';
am.FederationIdentifier = '[email protected]';
am.Username = '[email protected]' + System.currentTimeMillis();
am.CompanyName = 'TEST';
am.Title = 'title';
am.Alias = 'alias';
am.TimeZoneSidKey = 'America/Los_Angeles';
am.EmailEncodingKey = 'UTF-8';
am.LanguageLocaleKey = 'en_US';
am.LocaleSidKey = 'en_US';
insert am;
Account acc = new Account();
acc.OwnerId = so.Id;
acc.Name = 'Test Account';
acc.Customer_Success_Manage__c = am.Id;
acc.BillingCountry = 'India';
insert acc;
Account agc = new Account();
agc.Name = 'Test Agency';
agc.BillingCountry = 'India';
insert agc;
Contact con = new Contact();
con.FirstName = 'Test';
con.LastName = 'Contact';
con.AccountId = acc.Id;
insert con;
Opportunity opp = new Opportunity();
opp.Name = 'Main Opportunity';
opp.OwnerId = so.Id;
opp.ContactId = con.Id;
opp.AccountId = acc.Id;
opp.StageName = 'Proposal/Price Quote';
opp.CloseDate = System.today();
opp.Customer_Success_Manage__c = am.Id;
opp.Allow_New_Account_Combination__c = true;
opp.Billing_Request_Number__c = '778393';
opp.Campaign_Start_Date__c = System.today() + 4;
opp.Campaign_End_Date__c = System.today() + 6;
insert opp;
Product2 prod = new Product2();
prod.Name = 'InsightBase Enterprise';
prod.Description = 'Annual subscription,80 topics, 40,000 company locations';
prod.productCode = 'ABC';
prod.Service_Type__c = 'InsightBase';
prod.OB_Sub_Type_Name__c = 'InsightBase Enterprise';
prod.isActive = true;
insert prod;
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = prod.Id;
standardPrice.UnitPrice = 10000;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice;
//Test.startTest();
Order ord = new Order();
ord.Name = 'Test Order';
ord.AccountId = acc.Id;
ord.Pricebook2Id = Test.getStandardPricebookId();
ord.Customer_Success_Manager__c = am.Id;
ord.Agency_Name__c = agc.Id;
ord.OpportunityId = opp.Id;
ord.Purchase_Order__c = '783983';
ord.EffectiveDate = Date.today();
ord.Status = 'Draft';
ord.OBSyncStatus__c = 'Inactive';
ord.Billing_Instructions__c = 'Customer updated the reason';
ord.Billing_Type__c = 'Monthly Customer Approval';
ord.Pacing__c = 'Prorated - Even';
ord.End_Date__c = Date.today();
insert ord;
// prorated even scenario, middle of the month for both start and end date.
Test.startTest();
System.debug('standardPrice-Test: '+standardPrice.Id);
OrderItem oli = new OrderItem();
oli.OrderId = ord.Id;
oli.PricebookEntryId = standardPrice.Id;
oli.Quantity = 5;
oli.UnitPrice = 35;
oli.OBSyncStatus__c = 'Inactive';
oli.Line_Item_Start_Date__c = Date.newInstance(2025, 1, 15);
oli.Line_Item_End_Date__c = Date.newInstance(2025, 3, 15);
oli.Bonus_Units__c = 25;
oli.Geography__c = 'US';
oli.Billing_Type__c = 'Monthly Customer Approval';
oli.Pacing__c = 'Prorated - Even';
oli.Schedule_Automation_Status__c = 'Ready';
oli.Unique_Line_Item_Name__c = 'Insightbase Order';
insert oli;
OrderItem oli2 = new OrderItem();
oli2.OrderId = ord.Id;
oli2.PricebookEntryId = standardPrice.Id;
oli2.Quantity = 5;
oli2.UnitPrice = 35;
oli2.OBSyncStatus__c = 'Inactive';
oli2.Line_Item_Start_Date__c = Date.newInstance(2025, 1, 15);
oli2.Line_Item_End_Date__c = Date.newInstance(2025, 1, 29);
oli2.Bonus_Units__c = 25;
oli2.Geography__c = 'US';
oli2.Billing_Type__c = 'Monthly Customer Approval';
oli2.Pacing__c = 'Prorated - Even';
oli2.Schedule_Automation_Status__c = 'Ready';
oli2.Unique_Line_Item_Name__c = 'Insightbase Order';
insert oli2;
System.debug('details of order ' + ord + ' and the details of the order line ' + oli);
oli.Order_Line_Change_Reason__c = 'Delayed By Customer';
oli.Schedule_Automation_Status__c = 'Trigger Custom Billing Schedule';
update oli;
oli2.Order_Line_Change_Reason__c = 'Delayed By Customer';
oli2.Schedule_Automation_Status__c = 'Trigger Custom Billing Schedule';
update oli2;
Test.stopTest();
}
}
But the coverage of my test class is stopping at the line oli.Order.Billing_Type__c == 'Monthly Customer Approval'
I am not sure why this is happening as the value Monthly Customer Approval exists in the Billing Type picklist field of the Order object and the code is working fine, too, when I am testing it.
Can anyone please let me know why this is happening?