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
SFUserSFUser 

Hai..i am trying to cover the test coverage for below code.. but doesn't work..so how to cover test coverage for below switch statement lines of code. Thanks in Advance!.

switch on m {
                    when 1 {        
                        invoiceDate__c = 'Jan 1 to Jan 31';
                    }    
                    when 2 {        
                        invoiceDate__c = 'Feb 1 to Feb 28';
                    }
                    when 3 {
                        invoiceDate__c = 'Mar 1 to Mar 31';
                    }
                    when 4 {          
                        invoiceDate__c = 'Apr 1 to Apr 30';
                    }
                     when 5 {        
                        invoiceDate__c = 'May 1 to May 31';
                    }    
                    when 6 {        
                        invoiceDate__c = 'Jun 1 to Jun 30';
                    }
                    when 7 {
                        invoiceDate__c = 'Jul 1 to Jul 31';
                    }
                    when 8 {          
                        invoiceDate__c = 'Aug 1 to Aug 31';
                    }
                    when 9 {        
                        invoiceDate__c = 'Sep 1 to Sep 30';
                    }    
                    when 10 {        
                        invoiceDate__c = 'Oct 1 to Oct 31';
                    }
                    when 11 {
                        invoiceDate__c = 'Nov 1 to Nov 30';
                    }
                    when 12 {          
                        invoiceDate__c = 'Dec 1 to Dec 31';
                    }
                }
SUCHARITA MONDALSUCHARITA MONDAL

Hi Sukumar,

Use Date Formats to get is cover. Sharing the link for the same

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Hope this helps!
Thanks
Sucharita

SFUserSFUser
Thanks Sucharita.. but my actual code is mentioned below..and pls let me explain in detail how to cover this switch statement code
======================
Main Class
======================

global class ScheduleInvoice implements Schedulable {
    public static String CRON_EXP = '0 0 0 1 * ? *';
    global static String scheduleIt() {
        ScheduleInvoice sm = new ScheduleInvoice();
        return System.schedule('Monthly Reconciliation', CRON_EXP, sm);
    }
    global void execute(SchedulableContext sc) {Id AccountEIDrecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('EID').getRecordTypeId();
        Id AccountPVCrecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('PVC').getRecordTypeId();
        Id AccountPMCrecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('PMC').getRecordTypeId();
        Id AccountVMCrecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('VMC').getRecordTypeId();
        Id InvoiceVRCrecordTypeId = Schema.getGlobalDescribe().get('Invoice__c').getDescribe().getRecordTypeInfosByName().get('VRC').getRecordTypeId();
        
        set<ID> recordIds = new Set<ID>();
        recordIds.add(AccountEIDrecordTypeId);
        recordIds.add(AccountPVCrecordTypeId);
        recordIds.add(AccountPMCrecordTypeId);
        recordIds.add(AccountVMCrecordTypeId);
        
        List<Invoice__c> invoiceList = new List<Invoice__c>();
        List<Invoice__c> invList = new List<Invoice__c>();
        Integer m = Date.Today().Month();
        Date firstDayOfMonth = System.today().toStartOfMonth();
        Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1);
        Date invDesDate = system.Today().addMonths(1);
        Integer invMonth = invDesDate.Month();
        Integer invYear = invDesDate.year();
        String MonthName;
      
        for (Account acct: [
            select Id, Active__c RecordTypeId
            from Account
            where RecordTypeId IN: recordIds and Active__c = true
        ]) {
                Invoice__c inv = new Invoice__c();
                inv.Account__c = acct.Id;
                inv.Status__c = 'Accepted';
                inv.Date__c = system.Today();
                switch on m {
                    when 1 {        
                        invoiceDate__c = 'Jan 1 to Jan 31';
                    }    
                    when 2 {        
                        invoiceDate__c = 'Feb 1 to Feb 28';
                    }
                    when 3 {
                        invoiceDate__c = 'Mar 1 to Mar 31';
                    }
                    when 4 {          
                        invoiceDate__c = 'Apr 1 to Apr 30';
                    }
                     when 5 {        
                        invoiceDate__c = 'May 1 to May 31';
                    }    
                    when 6 {        
                        invoiceDate__c = 'Jun 1 to Jun 30';
                    }
                    when 7 {
                        invoiceDate__c = 'Jul 1 to Jul 31';
                    }
                    when 8 {          
                        invoiceDate__c = 'Aug 1 to Aug 31';
                    }
                    when 9 {        
                        invoiceDate__c = 'Sep 1 to Sep 30';
                    }    
                    when 10 {        
                        invoiceDate__c = 'Oct 1 to Oct 31';
                    }
                    when 11 {
                        invoiceDate__c = 'Nov 1 to Nov 30';
                    }
                    when 12 {          
                        invoiceDate__c = 'Dec 1 to Dec 31';
                    }
                }                                                                                                                
                   inv.Payment_Due_Date__c =  lastDayOfMonth; 
                switch on invMonth {
                    when 1 {        
                        MonthName = 'January';
                    }    
                    when 2 {        
                        MonthName = 'February';
                    }
                    when 3 {
                        MonthName = 'March';
                    }
                    when 4 {          
                        MonthName = 'April';
                    }
                     when 5 {        
                        MonthName = 'May';
                    }    
                    when 6 {        
                        MonthName = 'June';
                    }
                    when 7 {
                        MonthName = 'July';
                    }
                    when 8 {          
                        MonthName = 'August';
                    }
                    when 9 {        
                        MonthName = 'September';
                    }    
                    when 10 {        
                        MonthName = 'October';
                    }
                    when 11 {
                        MonthName = 'November';
                    }
                    when 12 {          
                        MonthName = 'December';
                    }
                }
            inv.RecordTypeId = InvoiceVRCrecordTypeId;
                inv.Description__c = MonthName + ' ' + String.valueof(invYear) + ' ' + 'TMC';
                
               invList.add(inv);
        }
          
        if (!invList.isEmpty()) {
            insert invList; 
        }   
    }
}

===========================
Test Class
===========================
@isTest
public class CreatingInvoice_Test {
    @isTest static void executeTest(){
        
        Test.startTest();
        
        Id AccountPVCrecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('PVC').getRecordTypeId();
        Id AccountPMCrecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('PMC').getRecordTypeId();
        Id AccountVMCrecordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('VMC').getRecordTypeId();
        Id InvoiceVRCrecordTypeId = Schema.getGlobalDescribe().get('Invoice__c').getDescribe().getRecordTypeInfosByName().get('VRC').getRecordTypeId();
        
        SchedulableContext sc = null;
        ScheduleInvoice CI = new ScheduleInvoice();
        CI.execute(sc);
        
        account a = new account();
        a.Name = 'Test Account';
        a.Active__c = true;
        a.RecordTypeId = AccountVMCrecordTypeId;
        
        insert a;
        
        ScheduleInvoice CI1 = new ScheduleInvoice();
        String sch1 = '0 0 0 1 * ? *'; 
        system.schedule('ScheduleInvoice', sch1, CI1);
        
        Test.stopTest();
    }
}