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
Manjunath reddy 25Manjunath reddy 25 

Need help in covering the code.

HI I need help in improving the code coverage for the below class.
public  class GSDAccountteammembercontroller {


public list<HPE_Account_Team__c> listofrecords{get;set;}
public Boolean TabToBeClosed {get;set;}
    
public PageReference SaveRecords() 
{
    string parentrecordid = ApexPages.currentPage().getParameters().get('Account__c');
    list<HPE_Account_Team__c> insertlist = new list<HPE_Account_Team__c>();  
    for(HPE_Account_Team__c acctem :listofrecords)
    {
        system.debug('acctem.Name__c'+acctem.Name__c);
        system.debug('parentrecordid'+parentrecordid);
        system.debug('listofrecords'+listofrecords);
        if(acctem.Name__c!=null)
        {
          if(parentrecordid != null && parentrecordid != '')
          acctem.Account__c = parentrecordid ;
          insertlist.add(acctem);
        
        }              
       
    }
        try{
        insert insertlist;
        }
        catch(Exception ex){
         ApexPages.addMessages(ex);
         return null;
        }       
        if(listofrecords!=null){
        listofrecords.clear();
        }
        TabToBeClosed = true;
        return null;

}

public GSDAccountteammembercontroller(ApexPages.StandardController controller){
 HPE_Account_Team__c HPEAccountTeam=(HPE_Account_Team__c)controller.getrecord();
 system.debug('HPEAccountTeam'+HPEAccountTeam);
TabToBeClosed = false;

listofrecords = new list<HPE_Account_Team__c>();
    
    for(Integer i=0; i<5; i++){   
         HPE_Account_Team__c gsdaccteam = new HPE_Account_Team__c();
         gsdaccteam.Account__c=HPEAccountTeam.Account__c;        
         gsdaccteam.Name__c=null;
         gsdaccteam.Role_Scope_Description__c='';
         gsdaccteam.Role__c='';
                 
         listofrecords.add(gsdaccteam);
    
         }
     
} 
  
}

My test class is as below.I am not able to cover the code few lines of the main class, thos lines area as below, find the attachement of screenshot.Screen shot taken from developer console.

 if(acctem.Name__c!=null)
        {
          if(parentrecordid != null && parentrecordid != '')
          acctem.Account__c = parentrecordid ;
          insertlist.add(acctem);
        
        }   
@isTest
public class GSDAccountteammembercontrollerTest{
    private static testMethod void SaveRecordTest(){
        account acc = new account();
        acc.name='test';
        insert acc;
        
        contact con = new contact();
            con.email = 'test@yahoo.com';
            con.Phone__c = '5423515415';
            con.LastName ='last Name';  
        insert con;
        system.debug('con'+con);
        
        HPE_Account_Team__c hpeAccountTeam = new HPE_Account_Team__c();
            hpeAccountTeam.Account__c = acc.id;
            hpeAccountTeam.Name__c = con.id;
            hpeAccountTeam.Role__c = 'Other';
            hpeAccountTeam.Role_Scope__c = 'Global';
            hpeAccountTeam.Role_Scope_Description__c = 'testing';
            hpeAccountTeam.Unique_Key__c = 'dfaafs';
            
            insert hpeAccountTeam;
    
  
        user u = [select id from user where id=:userInfo.getUserId()];
        
        System.runAs(u){
            ApexPages.StandardController con1 = new ApexPages.StandardController(hpeAccountTeam);
            GSDAccountteammembercontroller  teamMemberController = new GSDAccountteammembercontroller(con1);
            teamMemberController.SaveRecords();   
                

        }              
    }
}
Keyur  ModiKeyur Modi
Hi Manjunath, 

for if Section

if(parentrecordid != null && parentrecordid!= ''){
}

here "parentrecordid" is comming null because  you have not passed Account__c in parameter
 
 to pass your parameter  you need to pass it like below :- 
 
ApexPages.currentPage().getParameters().put('Account__c',acc.id);

To cover catch (exception ) you can use below code.
 
try{
        insert insertlist;
       Integer i  = ( test.isRunningTest() ? (1/0) : 1); // add this line in your try this will create exception when  test class is running
        }
        catch(Exception ex){
         ApexPages.addMessages(ex);
         return null;
        }

Please try this and let me know if this one will help you then.
If you need any help then please let me know.

Thanks,
Keyur Modi
Manjunath reddy 25Manjunath reddy 25
Hi Keyuri,

I have checked debug log.acctem.Name__c is coming as null, as it is becoming null in the constructor. 
that is why if is not satisfied.

if(acctem.Name__c!=null)
Manjunath reddy 25Manjunath reddy 25
User-added image

this is the debug log