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
sfdeveloper12sfdeveloper12 

How to increase code coverage of test class having if else condition?

Hello,

Code coverage of my test class is 54%. I want to cover if else condition. But i dont know how to cover if else condirtion in test class. 

My trigger and test class is as follows.

Apex Trigger:


trigger QuoteSynchingValidation on Quote (before insert,before update) {
     if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        
        
        String recordType;
        
        Id customerAccountRecordTypeId = Schema.SObjectType.Quote.getRecordTypeInfosByName().get('BenderINC - Industrial Quotes').getRecordTypeId();
   
        Boolean inRange ;

    
        for (Quote childObj : Trigger.new) {
            if(childObj.IsSyncing ) {
                 recordType = childObj.RecordTypeId; /* Record type is taken in formula filed on child object(QLI) as its originally on parent object(Q). */
                 if(recordType == customerAccountRecordTypeId )  {
                      if(childObj.Approval_Status__c == 'Awaiting Approval' || childObj.Approval_Status__c == 'Declined'){
                         childObj.addError('You cannot sync this Quote as Discount Approval is needed and has not been granted.');  
                      }
                      
                       
                      else 
                      {
       
                 checkSycnhhasclicked.hasClick = true;
                                     system.debug('59--->>checkSycnhhasclicked-->'+checkSycnhhasclicked.hasClick);
                      }
                 }
            }    
        }
         
    }
}


Test Class:

@isTest(seeAllData = true)
private class TestQuoteSynchingValidation {
 
    static testMethod void myUnitTest() {
   
    //Test Account Insert

    Account a = new Account();
    a.Name = 'Test Account';
    a.Account_Sector__c = 'Healthcare';
    a.Account_Type__c = 'Competitor';
    a.Country_List__c = 'England'; 
    a.BillingCity = 'Pune';
    a.BillingCountry = 'India';
    a.BillingState = 'MH';
    a.BillingStreet = 'Hadapsar';
    a.BillingPostalCode = '411028';
    insert a;

     Contact  c = new Contact();
     c.LastName = 'Patils';
     c.Title = 'Employee';
     c.LeadSource = 'Advertisement';
     c.AccountId = a.id;
     insert c;
     
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
   
   

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true];
    
    Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
    insert customPB;

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.isActive = true;
    //standardPrice.IsStandard = false;
    //standardPrice.UseStandardPrice = true;
    insert standardPrice ;
    
    PricebookEntry custompbe= new PricebookEntry();
    custompbe.Pricebook2Id = customPB.Id;
    custompbe.Product2Id = p.Id;
    custompbe.UnitPrice = 1;
    custompbe.IsActive = true;
   // custompbe.IsStandard = false;
    custompbe.UseStandardPrice = true;
    insert custompbe;
    
    //Test Order Insert
   
    Opportunity o = new Opportunity();
    o.Name = 'Test Order ';
    o.Type = 'Medical';
    //o.EffectiveDate = system.today();
    o.CloseDate= system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =customPB.Id ;
    o.LeadSource = 'Advertisement';
    o.ForecastCategoryName = 'Pipeline';
    o.StageName = 'Needs Analysis';
    o.Amount = 200.20 ;
    o.Products_Required__c = 'IMD';
    insert o;
   
    Quote i = new Quote();
    i.OpportunityId = o.id;
    i.Name= '170724508 - REV 001';
    i.Pricebook2Id = customPB.id;
    i.RecordTypeId = '012b00000001AxG';
    i.Approval_Status__c = 'Awaiting Approval';
    insert i;
     
    Quote i2 = new Quote();
    i2.OpportunityId = o.id;
    i2.Name= '170724508 - REV 001';
    i2.Pricebook2Id = customPB.id;
    i2.RecordTypeId = '012b00000001AxG';
    i2.Approval_Status__c = 'Declined';
    insert i2;
    
    QuoteLineItem Ql = new QuoteLineItem();
    Ql.QuoteId = i.id;
    Ql.Quantity = 24;
    Ql.UnitPrice = 240;
    Ql.Product2id = p.id;
    Ql.PricebookEntryId=custompbe.id;
    insert Ql;
    //QuoteLineItem  Q2 = [Select ];
    
    if(i2.IsSyncing )
       {
           i2.RecordTypeId = '012b00000001AxG';
           if(i2.RecordTypeId == '012b00000001AxG' )  
           {
               if(i2.Approval_Status__c == 'Declined'){
           
               i2.addError('You cannot sync this Quote as Discount Approval is needed and has not been granted.');  
           }
           }
       }
    
        } 
    
    static testMethod void myUnitTest2() 
    {
    
        Account a = new Account();
    a.Name = 'Test Account';
    a.Account_Sector__c = 'Healthcare';
    a.Account_Type__c = 'Competitor';
    a.Country_List__c = 'England'; 
    a.BillingCity = 'Pune';
    a.BillingCountry = 'India';
    a.BillingState = 'MH';
    a.BillingStreet = 'Hadapsar';
    a.BillingPostalCode = '411028';
    insert a;

     Contact  c = new Contact();
     c.LastName = 'Patils';
     c.Title = 'Employee';
     c.LeadSource = 'Advertisement';
     c.AccountId = a.id;
     insert c;
     
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
   
   

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true];
    
    Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
    insert customPB;

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.isActive = true;
    //standardPrice.IsStandard = false;
    //standardPrice.UseStandardPrice = true;
    insert standardPrice ;
    
    PricebookEntry custompbe= new PricebookEntry();
    custompbe.Pricebook2Id = customPB.Id;
    custompbe.Product2Id = p.Id;
    custompbe.UnitPrice = 1;
    custompbe.IsActive = true;
   // custompbe.IsStandard = false;
    custompbe.UseStandardPrice = true;
    insert custompbe;
    
    //Test Order Insert
   
    Opportunity o = new Opportunity();
    o.Name = 'Test Order ';
    o.Type = 'Medical';
    //o.EffectiveDate = system.today();
    o.CloseDate= system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =customPB.Id ;
    o.LeadSource = 'Advertisement';
    o.ForecastCategoryName = 'Pipeline';
    o.StageName = 'Needs Analysis';
    o.Amount = 200.20 ;
    o.Products_Required__c = 'IMD';
    insert o;
   
    Quote i = new Quote();
    i.OpportunityId = o.id;
    i.Name= '170724508 - REV 001';
    i.Pricebook2Id = customPB.id;
    i.RecordTypeId = '012b00000001AxG';
    i.Approval_Status__c = 'Awaiting Approval';
    insert i;
     
    Quote i2 = new Quote();
    i2.OpportunityId = o.id;
    i2.Name= '170724508 - REV 001';
    i2.Pricebook2Id = customPB.id;
    i2.RecordTypeId = '012b00000001AxG';
    i2.Approval_Status__c = 'Granted';
    insert i2;
    
    QuoteLineItem Ql = new QuoteLineItem();
    Ql.QuoteId = i.id;
    Ql.Quantity = 24;
    Ql.UnitPrice = 240;
    Ql.Product2id = p.id;
    Ql.PricebookEntryId=custompbe.id;
    insert Ql;
    
        if(i.IsSyncing )
       {
           i.RecordTypeId = '012b00000001AxG';
           if(i.RecordTypeId == '012b00000001AxG' )  
           {
               if(i.Approval_Status__c == 'Awaiting Approval'){
           
               i.addError('You cannot sync this Quote as Discount Approval is needed and has not been granted.');  
               }
            }
         }
    }
}

Can anyone please help me.

Thanks,
Utz
NagendraNagendra (Salesforce Developers) 
Hi Utkarsha,

May I suggest you please check with below link from stack exchange community with a similar issue which might guide you in the right direction. Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra
sfdeveloper12sfdeveloper12
Hi Nagendra,

I have changed my test class like this. But still code coverage is 54%.

@isTest(seeAllData = true)
private class TestcheckQuoteSynching {

    static testMethod void myUnitTest() {
   
    //Test Account Insert

    Account a = new Account();
    a.Name = 'Test Account';
    a.Account_Sector__c = 'Healthcare';
    a.Account_Type__c = 'Competitor';
    a.Country_List__c = 'England';
    a.BillingCity = 'Pune';
    a.BillingCountry = 'India';
    a.BillingState = 'MH';
    a.BillingStreet = 'Hadapsar';
    a.BillingPostalCode = '411028';
    insert a;

     Contact  c = new Contact();
     c.LastName = 'Patils';
     c.Title = 'Employee';
     c.LeadSource = 'Advertisement';
     c.AccountId = a.id;
     insert c;
     
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
   
   

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true];
    
    Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
    insert customPB;

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.isActive = true;
    //standardPrice.IsStandard = false;
    //standardPrice.UseStandardPrice = true;
    insert standardPrice ;
    
    PricebookEntry custompbe= new PricebookEntry();
    custompbe.Pricebook2Id = customPB.Id;
    custompbe.Product2Id = p.Id;
    custompbe.UnitPrice = 1;
    custompbe.IsActive = true;
   // custompbe.IsStandard = false;
    custompbe.UseStandardPrice = true;
    insert custompbe;
    
    //Test Order Insert
   
    Opportunity o = new Opportunity();
    o.Name = 'Test Order ';
    o.Type = 'Medical';
    //o.EffectiveDate = system.today();
    o.CloseDate= system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =customPB.Id ;
    o.LeadSource = 'Advertisement';
    o.ForecastCategoryName = 'Pipeline';
    o.StageName = 'Needs Analysis';
    o.Amount = 200.20 ;
    o.Products_Required__c = 'IMD';
    insert o;
   
    Quote i = new Quote();
    i.OpportunityId = o.id;
    i.Name= '170724508 - REV 001';
    i.Pricebook2Id = customPB.id;
    i.RecordTypeId = '012b00000001AxG';
    insert i;
    
    QuoteLineItem Ql = new QuoteLineItem();
    Ql.QuoteId = i.id;
    Ql.Quantity = 2;
    Ql.UnitPrice = 240;
    Ql.Product2id = p.id;
    Ql.PricebookEntryId=custompbe.id;
    Ql.Discount= 0.0;
  //  Ql.Discount= Null;
    insert Ql;
    
    QuoteLineItem Q2 = new QuoteLineItem();
    Q2.QuoteId = i.id;
    Q2.Quantity = 15;
    Q2.UnitPrice = 240;
    Q2.Product2id = p.id;
    Q2.PricebookEntryId=custompbe.id;
    Q2.Discount= 0.1;
    insert Q2;
    
    QuoteLineItem Q3 = new QuoteLineItem();
    Q3.QuoteId = i.id;
    Q3.Quantity = 25;
    Q3.UnitPrice = 240;
    Q3.Product2id = p.id;
    Q3.PricebookEntryId=custompbe.id;
    Q3.Discount= 0.3;
    insert Q3;
    
    QuoteLineItem Q4 = new QuoteLineItem();
    Q4.QuoteId = i.id;
    Q4.Quantity = 58;
    Q4.UnitPrice = 240;
    Q4.Product2id = p.id;
    Q4.PricebookEntryId=custompbe.id;
    Q4.Discount= 0.1;
    insert Q4;
    
     if(Q2.Quantity >= 0 && Q2.Quantity <= 9 &&  (Q2.Discount <= 0 || Q2.Discount == null) ) {
                            }
    
    
    }
    
    
    static testMethod void myUnitTest2() {
   
    //Test Account Insert

    Account a1 = new Account();
    a1.Name = 'Test Account';
    a1.Account_Sector__c = 'Healthcare';
    a1.Account_Type__c = 'Competitor';
    a1.Country_List__c = 'England';
    a1.BillingCity = 'Pune';
    a1.BillingCountry = 'India';
    a1.BillingState = 'MH';
    a1.BillingStreet = 'Hadapsar';
    a1.BillingPostalCode = '411028';
    insert a1;

     Contact  c1 = new Contact();
     c1.LastName = 'Patils';
     c1.Title = 'Employee';
     c1.LeadSource = 'Advertisement';
     c1.AccountId = a1.id;
     insert c1;
     
    Product2 p2 = new Product2();
    p2.Name = ' Test Product ';
    p2.Description='Test Product Entry 1';
    p2.productCode = 'ABC';
    p2.isActive = true;
    insert p2;
   
   

    Pricebook2  standardPb1 = [select id, name, isActive from Pricebook2 where IsStandard = true];
    
    Pricebook2 customPB1 = new Pricebook2(Name='Custom Pricebook', isActive=true);
    insert customPB1;

    PricebookEntry standardPrice1 = new PricebookEntry();
    standardPrice1.Pricebook2Id = standardPb1.Id;
    standardPrice1.Product2Id = p2.Id;
    standardPrice1.UnitPrice = 1;
    standardPrice1.isActive = true;
    //standardPrice.IsStandard = false;
    //standardPrice.UseStandardPrice = true;
    insert standardPrice1 ;
    
    PricebookEntry custompbe2= new PricebookEntry();
    custompbe2.Pricebook2Id = customPB1.Id;
    custompbe2.Product2Id = p2.Id;
    custompbe2.UnitPrice = 1;
    custompbe2.IsActive = true;
   // custompbe.IsStandard = false;
    custompbe2.UseStandardPrice = true;
    insert custompbe2;
    
    //Test Order Insert
   
    Opportunity o1 = new Opportunity();
    o1.Name = 'Test Order ';
    o1.Type = 'Medical';
    //o.EffectiveDate = system.today();
    o1.CloseDate= system.today() + 4;
    o1.AccountId = a1.id;
    o1.Pricebook2Id =customPB1.Id ;
    o1.LeadSource = 'Advertisement';
    o1.ForecastCategoryName = 'Pipeline';
    o1.StageName = 'Needs Analysis';
    o1.Amount = 200.20 ;
    o1.Products_Required__c = 'IMD';
    insert o1;
   
    Quote i1 = new Quote();
    i1.OpportunityId = o1.id;
    i1.Name= '170724508 - REV 001';
    i1.Pricebook2Id = customPB1.id;
    i1.RecordTypeId = '012b00000001AxG';
    insert i1;
    

    QuoteLineItem Q5 = new QuoteLineItem();
    Q5.QuoteId = i1.id;
    Q5.Quantity = 20;
    Q5.UnitPrice = 240;
    Q5.Product2id = p2.id;
    Q5.PricebookEntryId=custompbe2.id;
    Q5.Discount= 0.0;
  //  Ql.Discount= Null;
    insert Q5;
    
    QuoteLineItem Q6 = new QuoteLineItem();
    Q6.QuoteId = i1.id;
    Q6.Quantity = 15;
    Q6.UnitPrice = 240;
    Q6.Product2id = p2.id;
    Q6.PricebookEntryId=custompbe2.id;
    Q6.Discount= 0.8;
    insert Q6;
    
    QuoteLineItem Q7 = new QuoteLineItem();
    Q7.QuoteId = i1.id;
    Q7.Quantity = 25;
    Q7.UnitPrice = 240;
    Q7.Product2id = p2.id;
    Q7.PricebookEntryId=custompbe2.id;
    Q7.Discount= 0.45;
    insert Q7;
    
    QuoteLineItem Q8 = new QuoteLineItem();
    Q8.QuoteId = i1.id;
    Q8.Quantity = 58;
    Q8.UnitPrice = 240;
    Q8.Product2id = p2.id;
    Q8.PricebookEntryId=custompbe2.id;
    Q8.Discount= 0.41;
    insert Q8;

                            if(Q7.Quantity >= 0 && Q7.Quantity <= 9 &&  (Q7.Discount <= 0 || Q7.Discount == null) ) {
                            }
    
    
    }
    
}

Thanks & Best Regards,
Utkarsha