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
Akshay MhetreAkshay Mhetre 

Please help me to increase code coverage

@AuraEnabled
    public static void updateApplicationSeqNumber(String OpptyIdSet){
        
        Opportunity givenOpportunity = [Select Id, Application_Sequence_Number__c, Application_number__c from Opportunity where Id =: OpptyIdSet WITH SECURITY_ENFORCED];
       
            System_Configuration__c seqNumber = System_Configuration__c.getValues('System Configuration');

            //for(Opportunity op : opptyList){
                    
                if(seqNumber.Application_Date__c == System.today()){
                    if(seqNumber.Application_Sequence_No__c==null){
                        seqNumber.Application_Sequence_No__c=1;
                    }else{
                        seqNumber.Application_Sequence_No__c=seqNumber.Application_Sequence_No__c+1;
                    }
                    givenOpportunity.Application_Sequence__c = seqNumber.Application_Sequence_No__c;
                }
                else{
                    givenOpportunity.Application_Sequence__c=00001;
                    seqNumber.Application_Sequence_No__c=00001;
                    seqNumber.Application_Date__c =System.today();
                }
                
                String text=String.valueOf(givenOpportunity.Application_Sequence__c);
                text=text.substringBefore('.');
                
                text=text.leftPad(5, '0');  
                
                String todayYear = String.valueOf(System.today().Year()).right(2);
                String todayMonth = String.valueOf(system.today().Month()).length() == 1 ? '0' + String.valueOf(system.today().Month()) : String.valueOf(system.today().Month());
                String todayDay = String.valueOf(system.today().day()).length() == 1 ? '0' + String.valueOf(system.today().day()) : String.valueOf(system.today().day());
                String datePrefix = todayDay + '' + todayMonth + '' + todayYear;
                String finalLeadNumber = '';
                if(Test.isRunningTest() == false){
                    String applicationNameString = datePrefix +  text;

                    
                    Long appvalue = Long.valueOf(applicationNameString);
                    if (String.valueOf(appvalue).length() == 10) {
                        //adding '0' to Lead No pattern if date ranges in 01-09
                        finalLeadNumber = 'A' + '' + '0' + '' + String.valueOf(appvalue);
                    } else {
                        finalLeadNumber = 'A' + '' + String.valueOf(appvalue);
                    }
                    givenOpportunity.Application_number__c = finalLeadNumber;
                    
                }
           // }
            update seqNumber;
            update givenOpportunity;
            
        
    }

 

  @isTest
    public static void updateApplicationSeqNumberTest(){
        
        Opportunity opun1 = new Opportunity();
        opun1.StageName = 'Qualification';
        opun1.CloseDate = Date.newInstance(2021, 1, 11);
        opun1.Name = 'testone';
        insert opun1;
        
        Test.startTest();
        UniqueLeadNumberHandler.updateApplicationSeqNumber(opun1.Id);
        Test.stopTest();
    }

 

Best Answer chosen by Akshay Mhetre
AnkaiahAnkaiah (Salesforce Developers) 
Hi Akshay,

try with below code.
@isTest
    public static void updateApplicationSeqNumberTest(){
        
        Opportunity opun1 = new Opportunity();
        opun1.StageName = 'Qualification';
        opun1.CloseDate = Date.newInstance(2021, 1, 11);
        opun1.Name = 'testone';
		opun1.Application_Sequence__c = 1;
        insert opun1;
		
		//add the data for this custom settings
		 System_Configuration__c seqNumber = System_Configuration__c();
		 seqNumber.Application_Date__c = System.today();
		 seqNumber.Application_Sequence_No__c = 1;
		 insert seqNumber ;

        Test.startTest();
        UniqueLeadNumberHandler.updateApplicationSeqNumber(opun1.Id);
        Test.stopTest();
    }

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​