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
bcgbcg 

Facing problem in test method

Hello Everyone,

 

am facing some problem in test method code, actually am new to this and don't knw much about test method,  code coverage is done by this test method is 59%, but probelm am facing is ,So pls anyone check the code and tell me where am doing wrong, its giving error of :

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Candidate_Name__c, Father_Name__c]: [Candidate_Name__c, Father_Name__c]
 

Please check the same if anyone knows....code of class is given below:

/*This apex class is used to save the Entrance Registration Data Entrance Registration Object, and Educational Information
of student. And educational information data is saving in Educational Information object.*/

//Apex Class
public class EntranceRegistrationCls{
 
    //Variable Declaration
    public String MasterId = '';
    public String EduRecordID;
    String TabURL;
    
    //Getter Setter Property Declaration
    public List<Educational_Information__c> EduList{get; set;}
    public Educational_Information__c Education{get;set;}
    
    //List Delcaration
    public list<Educational_Information__c> delList = new list<Educational_Information__c>();   
    public Entrance_Registration__c EntranceReg = new Entrance_Registration__c();
    public Entrance_Registration__c CurrentEntranceReg {get;set;}     
    ApexPages.StandardController controller;
    
    //constructor of EntranceRegistrationCls class
    public EntranceRegistrationCls(ApexPages.StandardController controller){        
        try{
            system.debug('yess');
            this.controller = controller;
            EntList = new List<Entrance_Registration__c>();
            EduList= new List<Educational_Information__c>();        
            EduList.add(new Educational_Information__c());
            Education= new Educational_Information__c();
            getMasterId();            
                if(MasterId == null || MasterId == ''){
                }else{
                    system.debug('Truee..>>'+MasterId);     
                    EntranceReg =[SELECT Id,Name FROM Entrance_Registration__c WHERE Id =:MasterId];
                    EduList=  [SELECT Id,Board_University__c,Degree_Diploma_Certificate_Etc_specify__c, Year_of_Passing__c,Education__c,Institute_College__c,Marks_CGPA__c,Mode_of_Education__c,
                               Status__c FROM Educational_Information__c WHERE Entrance_Master_Id__c =: EntranceReg.Id ];
                    system.debug('Yesssss--->>'+EduList);
                } 
        }
        catch(Exception ex){  
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(myMsg);
        }
    }
    //End of Constructor
    
    //Get Method for getting the current page id of Entrance Registration    
    public String getMasterId() {
        if(MasterId == null || MasterId == '')
            MasterId= ApexPages.CurrentPage().getParameters().get('ID');
            return MasterId;
    }
    //End of Get Method
    
    //Set Method for setting the value of Enratnce Registration    
    public void setMasterId(String val){
        MasterId = val;
    }
    //End of Set Method
    
    //To add any row method  
    public void addrow() {                              
        for(Integer i=0;i<EduList.size();i++){           
            EduList.add(new Educational_Information__c());                 
            return;
        }
    }
    //To remove any row method        
    public void removeRow(){  
        Integer i = EduList.size();
        if(EduList[i-1].Id <> null)
        delList.add(EduList[i-1]);
        EduList.remove(i-1); 
    }  
    // Save method to save whole data
    public PageReference save(){        
        Schema.DescribeSObjectResult SObjectResult = Schema.SObjectType.Entrance_Registration__c;
        String ObjectKey = SObjectResult.getKeyPrefix();
        TabURL = '/'+ ObjectKey +'/o';
        system.debug('Yes....>>'+TabURL);
            try{
                EntranceReg = (Entrance_Registration__c)controller.getRecord();
                CurrentEntranceReg = EntranceReg;
                system.debug('Yuuuupp>>>>'+CurrentEntranceReg);
                        
            }
            catch (Exception ex){
                ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
                ApexPages.addMessage(errMsg);
                return null;
            }
            try{
                if(CurrentEntranceReg.id <> null){
                        update CurrentEntranceReg;
                } else{
                        insert CurrentEntranceReg;
                  }
                  system.debug('Yes'+EduList);
                  
                for(Educational_Information__c ast: EduList){
                    if(ast.Id == null) 
                        ast.Entrance_Master_Id__c =  CurrentEntranceReg.ID;                        
                        ast.Education__c = ast.Education__c;
                        system.debug('Trueeeee...>'+ast.Entrance_Master_Id__c);
                        ast.Board_University__c = ast.Board_University__c;
                        ast.Institute_College__c = ast.Institute_College__c;
                        ast.Marks_CGPA__c = ast.Marks_CGPA__c;
                        ast.Mode_of_Education__c = ast.Mode_of_Education__c;
                        ast.Status__c = ast.Status__c;
                        ast.Year_of_Passing__c = ast.Year_of_Passing__c;
                        ast.Degree_Diploma_Certificate_Etc_specify__c=ast.Degree_Diploma_Certificate_Etc_specify__c;                    
                 }
                 system.debug('TRUE--->'+EduList);   
                upsert EduList;          
            }
            catch (Exception ex){
                ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
                ApexPages.addMessage(errMsg);
                return null;
            }            
            PageReference home = new PageReference(TabURL);        
            home.setRedirect(true);        
            return home;                                                
    }
    //End of Save Method
    static testMethod void testEntranceReg(){
        
        Test.startTest();
        PageReference PageRef = Page.EntranceRegistrationVf;
        test.setCurrentPage(PageRef);
        
       ApexPages.StandardController thecontroller = new ApexPages.StandardController(new Entrance_Registration__c());
       EntranceRegistrationCls controller = new EntranceRegistrationCls(thecontroller);
       controller.addrow();
       controller.removeRow();
        
        for(Entrance_Registration__c Ent:controller.CurrentEntranceReg){
            Ent.Candidate_Name__c ='Neeru';
            Ent.Father_Name__c ='Mr. Rakesh Nagpal';
        }
        controller.save();
        Test.stopTest();    
    }
        /*Entrance_Registration__c[] EntReg = new Entrance_Registration__c[]{
            new Entrance_Registration__c(
                Candidate_Name__c='Test Name',Father_Name__c='Test Father Name')};
         insert EntReg;
         
       ApexPages.StandardController thecontroller = new ApexPages.StandardController(new Entrance_Registration__c());
       EntranceRegistrationCls controller = new EntranceRegistrationCls(thecontroller);
       EntranceRegistrationCls  Ent =new EntranceRegistrationCls (thecontroller);
       Ent.save();
       Ent.addrow();
       Ent.removeRow(); */           
   
   /* static testMethod void myUnitTest() {
        PageReference PageRef = Page.EntranceRegistrationVf;
        test.setCurrentPage(PageRef); 
        Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(new Entrance_Registration__c());
        EntranceRegistrationCls controller = new EntranceRegistrationCls(sc);
        String MasterId;
        controller.setMasterId(MasterId);
        controller.addrow();
        controller.removeRow();
        controller.save();
    }*/
 
}
//End of Apex Class

 

when am saving this its giving error :

Compile Error: Loop must iterate over a collection type: SOBJECT:Entrance_Registration__c at line 140 column 42

Thanks In Advance

Pratibh PrakashPratibh Prakash

Hello 

 

On line 140 you are using controller.CurrentEntranceReg, which is not  a list type of property.

For loop expects a List of Entrance_Registration__c type but CurrentEntranceReg is an instance of Entrance_Registration__c refer line no. 19.

So please change you for loop to reference a collection.

bcgbcg

Hey,

 

Thanks For Reply.

 

Actually , i knw the error that you wrote but don't knw how to solve that. if you knw then pls post here..........

 

 

Thanks In Advance

bcgbcg

Now am using the code like this, i.e.. Using List with controller. But still facing the error . and please check the below code and check modifications and is anyone aware of the error then please post the solution for the same.

|Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Candidate_Name__c, Father_Name__c]:

/*This apex class is used to save the Entrance Registration Data Entrance Registration Object, and Educational Information
of student. And educational information data is saving in Educational Information object.*/

//Apex Class
public class EntranceRegistrationCls{
 
    //Variable Declaration
    public String MasterId = '';
    public String EduRecordID;
    String TabURL;
    
    //Getter Setter Property Declaration
    public List<Educational_Information__c> EduList{get; set;}
    public List<Entrance_Registration__c> EntList{get; set;}   
    public Educational_Information__c Education{get;set;}
    
    //List Delcaration
    public list<Educational_Information__c> delList = new list<Educational_Information__c>();   
    public Entrance_Registration__c EntranceReg = new Entrance_Registration__c();
    public Entrance_Registration__c CurrentEntranceReg ;     
    ApexPages.StandardController controller;
    
    //constructor of EntranceRegistrationCls class
    public EntranceRegistrationCls(ApexPages.StandardController controller){        
        try{
            system.debug('yess');
            this.controller = controller;
            EntList = new List<Entrance_Registration__c>();
            EntList.add(new Entrance_Registration__c());
            EduList= new List<Educational_Information__c>();        
            EduList.add(new Educational_Information__c());
            Education= new Educational_Information__c();
            getMasterId();            
                if(MasterId == null || MasterId == ''){
                }else{
                    system.debug('Truee..>>'+MasterId);     
                    EntranceReg =[SELECT Id,Name FROM Entrance_Registration__c WHERE Id =:MasterId];
                    EduList=  [SELECT Id,Board_University__c,Degree_Diploma_Certificate_Etc_specify__c, Year_of_Passing__c,Education__c,Institute_College__c,Marks_CGPA__c,Mode_of_Education__c,
                               Status__c FROM Educational_Information__c WHERE Entrance_Master_Id__c =: EntranceReg.Id ];
                    system.debug('Yesssss--->>'+EduList);
                } 
        }
        catch(Exception ex){  
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(myMsg);
        }
    }
    //End of Constructor
    
    //Get Method for getting the current page id of Entrance Registration    
    public String getMasterId() {
        if(MasterId == null || MasterId == '')
            MasterId= ApexPages.CurrentPage().getParameters().get('ID');
            return MasterId;
    }
    //End of Get Method
    
    //Set Method for setting the value of Enratnce Registration    
    public void setMasterId(String val){
        MasterId = val;
    }
    //End of Set Method
    
    //To add any row method  
    public void addrow() {                              
        for(Integer i=0;i<EduList.size();i++){           
            EduList.add(new Educational_Information__c());                 
            return;
        }
    }
    //To remove any row method        
    public void removeRow(){  
        Integer i = EduList.size();
        if(EduList[i-1].Id <> null)
        delList.add(EduList[i-1]);
        EduList.remove(i-1); 
    }  
    // Save method to save whole data
    public PageReference save(){        
        Schema.DescribeSObjectResult SObjectResult = Schema.SObjectType.Entrance_Registration__c;
        String ObjectKey = SObjectResult.getKeyPrefix();
        TabURL = '/'+ ObjectKey +'/o';
        system.debug('Yes....>>'+TabURL);
            try{
                EntranceReg = (Entrance_Registration__c)controller.getRecord();
                CurrentEntranceReg = EntranceReg;
                system.debug('Yuuuupp>>>>'+CurrentEntranceReg);
                        
            }
            catch (Exception ex){
                ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
                ApexPages.addMessage(errMsg);
                return null;
            }
            try{
                if(CurrentEntranceReg.id <> null){
                        update CurrentEntranceReg;
                } else{
                        insert CurrentEntranceReg;
                  }
                  system.debug('Yes'+EduList);
                  EntList.add(CurrentEntranceReg);
                  system.debug('^^^^^^^List^^^^^^^'+EntList);
                for(Educational_Information__c ast: EduList){
                    if(ast.Id == null) 
                        ast.Entrance_Master_Id__c =  CurrentEntranceReg.ID;                        
                        ast.Education__c = ast.Education__c;
                        system.debug('Trueeeee...>'+ast.Entrance_Master_Id__c);
                        ast.Board_University__c = ast.Board_University__c;
                        ast.Institute_College__c = ast.Institute_College__c;
                        ast.Marks_CGPA__c = ast.Marks_CGPA__c;
                        ast.Mode_of_Education__c = ast.Mode_of_Education__c;
                        ast.Status__c = ast.Status__c;
                        ast.Year_of_Passing__c = ast.Year_of_Passing__c;
                        ast.Degree_Diploma_Certificate_Etc_specify__c=ast.Degree_Diploma_Certificate_Etc_specify__c;                    
                 }
                 system.debug('TRUE--->'+EduList);   
                upsert EduList;          
            }
            catch (Exception ex){
                ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
                ApexPages.addMessage(errMsg);
                return null;
            }            
            PageReference home = new PageReference(TabURL);        
            home.setRedirect(true);        
            return home;                                                
    }
    //End of Save Method
    static testMethod void testEntranceReg(){
        
        Test.startTest();
        /*Entrance_Registration__c[] EntReg = new Entrance_Registration__c[]{
            new Entrance_Registration__c(
                Candidate_Name__c='Neeru Nagpal',Father_Name__c='Mr. Rakesh Nagpal')};
         insert EntReg;
         
        pagereference pg=new pagereference('/apex/EntanceRegistrationVf'); 
        system.test.setCurrentpage(pg); 
        //object Entrance_Registration__c=new object();
        ApexPages.StandardController sc = new ApexPages.StandardController(new Entrance_Registration__c());
        EntranceRegistrationCls cn = new EntranceRegistrationCls(sc);
        //cn.property1 = pg.getparameters().get('id');
        cn.addrow();
        cn.removeRow();
        cn.save();*/

        PageReference PageRef = Page.EntranceRegistrationVf;
        test.setCurrentPage(PageRef);
        
       ApexPages.StandardController thecontroller = new ApexPages.StandardController(new Entrance_Registration__c());
       EntranceRegistrationCls controller = new EntranceRegistrationCls(thecontroller);
       controller.addrow();
       controller.removeRow();
       String MasterId;
       controller.setMasterId(MasterId);
 
        for(Entrance_Registration__c Ent:controller.EntList){
            Ent.Candidate_Name__c ='Test Name';
            Ent.Father_Name__c ='Test Father Name';
        }
        controller.save();
        Test.stopTest();    
    }              
   
   /* static testMethod void myUnitTest() {
        PageReference PageRef = Page.EntranceRegistrationVf;
        test.setCurrentPage(PageRef); 
        Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(new Entrance_Registration__c());
        EntranceRegistrationCls controller = new EntranceRegistrationCls(sc);
        String MasterId;
        controller.setMasterId(MasterId);
        controller.addrow();
        controller.removeRow();
        controller.save();
    }*/
 
}
//End of Apex Class

 

 

 

bcgbcg

Hello Pratibh,

 

 

Will you please tell me its solution if you know.?

 

Thanks In Advance

Pratibh PrakashPratibh Prakash

The error already states what needs to performed. When you are creating data in the test class please populate some value in Candidate_Name__c field as this is required field which throws up error when DML is performed.