+ Start a Discussion
yarramyarram 

Urgent: not able to do code coverage for "FOR loop 'IF statement'"

Hi all,

i have two custon objects (Case_Application__c(master) and Education_Application_Detail__c(detail)) these both has master-detail relation ship. here what i have to do --- if master object record doesn't have the child records then i have to create child(detail) record with master object record data automatically. i have done this but i am not able to do the code coverage for this logic. below is my class code how can i write the test class for this atleast 80% code coverage. Please help me on this.

public with sharing class RecordTypeCtrl {
     public ID eadRecordTypeId {get;set;}
     List<Case_Application__c> caList;

    public RecordTypeCtrl(){   
           
            eadRecordTypeId = Schema.SObjectType.Case_Application__c.RecordTypeInfosByName.get('Education').RecordTypeId;       
            if(eadRecordTypeId!=null){
                    eadDetails();
            }
    }

  
    public void eadDetails(){
        
         caList=[select id,name,recordtype.name,Award__c,
                                 (select id,name,Case_Application__c from Education_Application_Details__r),Case_Application_Status__c,Case_File__c,
                                                                 Course_Type__c,Details_of_Exceptional_Need__c,Exceptional_Needs_Cost__c,
                                                                 Purpose__c,Sign_Language_Interpreting_Cost__c from Case_Application__c
                                                                 where RecordTypeId=:eadRecordTypeId];
          
          List<Education_Application_Detail__c> eadList=new List<Education_Application_Detail__c>();
          
          for(Case_Application__c ca: caList){
               
              if(ca.Education_Application_Details__r.size()==0){ ----------//from here not cover
               
                   Education_Application_Detail__c ead=new Education_Application_Detail__c();
                        ead.Case_Application__c=ca.Id;
                        ead.Course_Type__c=ca.Course_Type__c;
                        ead.Award__c=ca.Award__c;
                         
                        eadList.add(ead);
              }
-------------/// Upto here not cover
          }
          if(eadList.size()>0){
                insert eadList;
         }

    
    }
}

please help me on this

Thanks,
Yarram.
ManojjenaManojjena
Hi Yarram,

Is possible to post your test code ? I will try to modify that .
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Make sure that you are populating your test data for below objects.

1. Case_Application__c with Education record type.
2. Education_Application_Details__c

Let us know if it helps you.
ManojjenaManojjena
Hi Yarram,

Can you please tell me the mandatory fields in Education_Application_Detail__c to insert record.
 
yarramyarram
Hi Manoj,
thanks for your reply and
Case_Application__c field is mandatory on this object.
@isTest
private class RecordTypeCtrlTestSuite {
     
     static testMethod void previousCAEADTest() {

        ID eadRecordTypeId = Schema.SObjectType.Case_Application__c.RecordTypeInfosByName.get('Education, Learning & Development').RecordTypeId;
        
        Case_Application__c ca1=new Case_Application__c(Name='Test Case Application',
                                                        Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca1;
        Case_Application__c ca2=new Case_Application__c(Name='Test Case Application1',
                                                        Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca2;
        Case_Application__c ca3=new Case_Application__c(Name='Test Case Application1',
                                                        Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca3;

        Education_Application_Detail__c tead1=new Education_Application_Detail__c(Case_Application__c=ca1.Id,Course_Type__c='Third Level1');
        insert tead1;
        
        Education_Application_Detail__c tead2=new Education_Application_Detail__c(Case_Application__c=ca1.Id,Course_Type__c='Third Level3');
        insert tead2;
        
        
        List<Case_Application__c> caList=[select id,name,recordtype.name,Award__c,Case_Application_Status__c,Case_File__c,
                                                 (select id,name,Case_Application__c from Education_Application_Details__r),
                                                             Course_Type__c,Details_of_Exceptional_Need__c,Exceptional_Needs_Cost__c,
                                                             Purpose__c,Sign_Language_Interpreting_Cost__c from Case_Application__c
                                                             where RecordTypeID=:eadRecordTypeId ];

         List<Education_Application_Detail__c> eadList=new List<Education_Application_Detail__c>();
          for(Case_Application__c ca:caList){
                
              if(ca.Education_Application_Details__r.size()==0){
               
                   Education_Application_Detail__c ead=new Education_Application_Detail__c();
                        ead.Case_Application__c=ca.Id;// This is mandotory field
                        ead.Course_Type__c=ca.Course_Type__c;                         
                        eadList.add(ead);
              }
          }
          if(eadList.size()>0){
                insert eadList;
         }
          Test.starttest();
               RecordTypeCtrl rt=new RecordTypeCtrl();
                   rt.eadDetails();
                  
         Test.stoptest();    
     }
     
}
please tell me where i have to do the changes in my test class.

Thanks,
Yarram.

 
ManojjenaManojjena
Hi Yarram,

Please check below code
@isTest
private class RecordTypeCtrlTestSuite {
   static testMethod void previousCAEADTest() {
     List<Case_Application__c> casAppList=new List<Case_Application__c>();
      ID eadRecordTypeId = Schema.SObjectType.Case_Application__c.RecordTypeInfosByName.get('Education, Learning & Development').RecordTypeId;
      Case_Application__c ca1=new Case_Application__c(Name='Test Case Application',Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca1;
			 casAppList.add(ca1);
        Case_Application__c ca2=new Case_Application__c(Name='Test Case Application1',Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca2;
			casAppList.add(ca2);
        Case_Application__c ca3=new Case_Application__c(Name='Test Case Application1', Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
           insert ca3;
		   casAppList.add(ca3);
	Education_Application_Detail__c ead=new Education_Application_Detail__c();
	 ead.Case_Application__c=ca1.Id;
	 Education_Application_Detail__c ead1=new Education_Application_Detail__c();
	 ead1.Case_Application__c=ca1.Id;
	 Test.startTest();
		  RecordTypeCtrl rt=new RecordTypeCtrl();
		  rt.caList=casAppList;
		  rt.eadDetails();
	  Test.startTest();
   }
}

Alos you need to make the changes to your class .
@TestVisible List<Case_Application__c> caList;
 
ManojjenaManojjena
Hi Yarram,

Please check again below code .
@isTest
private class RecordTypeCtrlTestSuite {
   static testMethod void previousCAEADTest() {
     List<Case_Application__c> casAppList=new List<Case_Application__c>();
      ID eadRecordTypeId = Schema.SObjectType.Case_Application__c.RecordTypeInfosByName.get('Education, Learning & Development').RecordTypeId;
      Case_Application__c ca1=new Case_Application__c(Name='Test Case Application',Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca1;
			 casAppList.add(ca1);
        Case_Application__c ca2=new Case_Application__c(Name='Test Case Application1',Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca2;
			casAppList.add(ca2);
        Case_Application__c ca3=new Case_Application__c(Name='Test Case Application1', Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
           insert ca3;
		   casAppList.add(ca3);
	Education_Application_Detail__c ead=new Education_Application_Detail__c();
	 ead.Case_Application__c=ca1.Id;
	 Education_Application_Detail__c ead1=new Education_Application_Detail__c();
	 ead1.Case_Application__c=ca1.Id;
	 Test.startTest();
		  RecordTypeCtrl rt=new RecordTypeCtrl();
		  rt.caList=casAppList;
		  rt.eadDetails();
	  Test.startTest();
   }
   static testMethod void previousCAEADTestOne() {
     List<Case_Application__c> casAppList=new List<Case_Application__c>();
      ID eadRecordTypeId = Schema.SObjectType.Case_Application__c.RecordTypeInfosByName.get('Education, Learning & Development').RecordTypeId;
      Case_Application__c ca1=new Case_Application__c(Name='Test Case Application',Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca1;
			 casAppList.add(ca1);
        Case_Application__c ca2=new Case_Application__c(Name='Test Case Application1',Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
            insert ca2;
			casAppList.add(ca2);
        Case_Application__c ca3=new Case_Application__c(Name='Test Case Application1', Course_Type__c='Third Level',RecordTypeID=eadRecordTypeId);
           insert ca3;
		   casAppList.add(ca3);
	 Test.startTest();
		  RecordTypeCtrl rt=new RecordTypeCtrl();
		  rt.caList=casAppList;
		  rt.eadDetails();
	  Test.startTest();
   }
}

I think it will help you .