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
Gopal ChatGopal Chat 

My test class is not covering hole code

trigger AppointmentConfirmdateChangeFormat on Appointments__c (before insert,before update) {
    for(Appointments__c app :trigger.new){
        if(app.Confirmed_Date_of_Appointment__c!=Null){            
            app.Confirmed_DateofAppointment__c = string.valueOf(app.Confirmed_Date_of_Appointment__c);  
        }
        
        if(trigger.isInsert){
            String lastDigit = '';
            List<String> nameFormat = new List<String>();
            List<Appointments__c> appn = [SELECT Id, Name, CreatedDate FROM Appointments__c 
                                          WHERE CreatedDate =THIS_YEAR  ORDER BY CreatedDate DESC LIMIT 1];
            Integer i=0;
            string year=string.valueOf(System.today().year());
            string test= year+'-';
            if(appn.size()>0 && appn[0].Name.contains(test)){
                if(appn[0].Name != Null)
                    nameFormat = String.valueOf(appn[0].Name).Split('-');
                lastDigit = nameFormat[1];
                i = Integer.valueOf(lastDigit);
                system.debug('>>>>>>>>>'+i);
            }  
            for(Appointments__c appoint:Trigger.new){
                
                if(appn.size()>0 && appn[0].Name.contains(test)){
                    if(i>0 && i<9){
                        i +=1;
                        appoint.Name = year+'-'+'0000'+String.ValueOf(i);
                    }
                    else if(i>=9 && i<99){
                        i +=1;
                        appoint.Name = year+'-'+'000'+String.ValueOf(i);
                    }
                    else if(i>=99 && i<999){
                        i +=1;
                        appoint.Name = year+'-'+'00'+String.ValueOf(i);
                    }
                    else if(i>=999 && i<9999){
                        i +=1;
                        appoint.Name = year+'-'+'0'+String.ValueOf(i);
                    }
                    else if(i>=9999){
                        i+=1;
                        appoint.Name = year+'-'+String.ValueOf(i);
                    }
                }
                else{
                    appoint.Name = year+'-'+'00001';
                }
                
                system.debug('appoint.Name----------->'+appoint.Name);    
            }
        }
    }
    
    
}



@isTest
public class AppointmentConfirmdateChangeFormatTest {
    
    @isTest
    public static void testMthd(){
        Id acId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Non-Accredited Doctors').getRecordTypeId();
        Account acc = new Account();
        acc.RecordTypeId = acId;
        acc.Name = 'Test';
        insert acc;
        
        Id docRecord = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Doctors').getRecordTypeId();
        
        
        Account testAcc3 = new Account();
        testAcc3.RecordTypeId = docRecord;
        testAcc3.LastName = 'Test Account';
        testAcc3.Gender__c = 'Male';
        testAcc3.Nationality__c = 'Singapore';
        insert testAcc3;
        
        Id caseId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('Business Development').getRecordTypeId();
        Case c = new Case();
        c.RecordTypeId = caseId;
        c.AccountId = acc.Id;
        c.Actual_Request_Date__c = System.today();
        c.Origin = 'Phone';
        c.Status = 'Closed';
        insert c;
        
        
        Appointments__c appt = new Appointments__c();
        appt.Name=system.today().year()+'-'+'00099';
        appt.Case_Ref__c = c.Id;
        appt.Doctors__c = testAcc3.id;
        appt.Confirmed_Date_of_Appointment__c = System.now();
        insert appt;
        
        
        Appointments__c appt1 = new Appointments__c();
        appt1.Name=system.today().year()+'-'+'00099';
        appt1.Case_Ref__c = c.Id;
        appt1.Doctors__c = testAcc3.id;
        appt1.Confirmed_Date_of_Appointment__c = System.now();
        insert appt1;
        
        appt.Name=system.today().year()+'-'+'00098';
        update appt;
        
        Appointments__c appt2 = new Appointments__c();
        appt2.Name=system.today().year()+'-'+'00999';
        appt2.Case_Ref__c = c.Id;
        appt2.Doctors__c = testAcc3.id;
        appt2.Confirmed_Date_of_Appointment__c = System.now();
        insert appt2;

        appt2.Name=system.today().year()+'-'+'00998';
        update appt;
                
    }
}
Kai Herng LauKai Herng Lau
Hi Raghav Chaturvedi,

If I'm not mistaken your test class will not cover all the code in here
                    if(i>0 && i<9){
                        i +=1;
                        appoint.Name = year+'-'+'0000'+String.ValueOf(i);
                    }
                    else if(i>=9 && i<99){
                        i +=1;
                        appoint.Name = year+'-'+'000'+String.ValueOf(i);
                    }
                    else if(i>=99 && i<999){
                        i +=1;
                        appoint.Name = year+'-'+'00'+String.ValueOf(i);
                    }
                    else if(i>=999 && i<9999){
                        i +=1;
                        appoint.Name = year+'-'+'0'+String.ValueOf(i);
                    }
                    else if(i>=9999){
                        i+=1;
                        appoint.Name = year+'-'+String.ValueOf(i);
                    }

I think this is because your test data for Application__c name is all set to system.today().year()+'-'+'00099';

My suggestion for you is to update your test data for Application__c.Name to
 
....        
        Appointments__c appt = new Appointments__c();
        appt.Name=system.today().year()+'-1';
        appt.Case_Ref__c = c.Id;
        appt.Doctors__c = testAcc3.id;
        appt.Confirmed_Date_of_Appointment__c = System.now();
        insert appt;
        
        
        Appointments__c appt1 = new Appointments__c();
        appt1.Name=system.today().year()+'-99';
        appt1.Case_Ref__c = c.Id;
        appt1.Doctors__c = testAcc3.id;
        appt1.Confirmed_Date_of_Appointment__c = System.now();
        insert appt1;
        
        appt.Name=system.today().year()+'-98';
        update appt;
        
        Appointments__c appt2 = new Appointments__c();
        appt2.Name=system.today().year()+'-999';
        appt2.Case_Ref__c = c.Id;
        appt2.Doctors__c = testAcc3.id;
        appt2.Confirmed_Date_of_Appointment__c = System.now();
        insert appt2;

        appt2.Name=system.today().year()+'-998';
        update appt;

Can you give it a try and let me know if this is working. Thanks