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
Mamadou Diallo 16Mamadou Diallo 16 

Test failing for inner class (Help needed please!!!!!!!!!!)

Hello,

I need help. Here is the controller and the test class
public class AccountAndContactsGridExtension 
{
    public Account currentAccount {get; set;}
    public ApexPages.StandardController std;
    
    // the associated contacts
    public List<Contact> contacts;
    // the chosen contact id - used when deleting a contact
    public Id chosenContactId {get; set;}
    public String newContactFirstName {get; set;}
    public String newContactLastName {get; set;}
    public boolean ShowAddRoles{get; set;}
    public AccountAndContactsGridExtension()
    {
        ShowAddRoles = false;
    }
     
    public void doProcess(){
       ShowAddRoles = true;
    } 
        public AccountAndContactsGridExtension(ApexPages.StandardController stdCtrl){
            std=stdCtrl;
            lstInner = new List<innerClass>();
            lstInner2 = new List<innerClass>();
            selectedRowIndex = '0';
            currentAccount = (Account) std.getRecord();
            System.Debug('#######current Account Id:' + currentAccount.Id); 
            addMore();
        }
         
        public Account getAccount(){
         return (Account) std.getRecord();
        }
 
        public SObject getSobject(){
            return std.getRecord();
        }
      
        public boolean updateContacts()
        {
            boolean result=true;
            if (null!=contacts)
               {
                 List<Contact> updConts=new List<Contact>();
                  try
                  {
                      update contacts;
                      if(Test.isRunningTest())
                      Integer intTest =20/0;
                                           
                  }
                  catch (Exception e)
                  {
                     String msg=e.getMessage();
                     integer pos;
                      
                     // if its field validation, this will be added to the messages by default
                     if (-1==(pos=msg.indexOf('MailState_2digit_code, ')))
                     {
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
                     }
                      
                     result=false;
                  }
               }
                
               return result;
        }
     
        public PageReference saveAndExit()
        {
             boolean result=true;
           //  boolean result1=true;
             result=updateContacts();
              
             if (result){
                // call standard controller save
                return std.save();
             }
             else
             {
              return null;
             }
        }
    
        public void newContact()
        {
           if (updateContacts())
           {
              Contact cont=new Contact(FirstName=newContactFirstName, LastName=newContactLastName, AccountId=getAccount().id);
              insert cont;
             
              newContactFirstName=null;
              newContactLastName=null;
              contacts=null;
           }
        }
    
         
        public void deleteContact()
        {
           if (updateContacts())
           {
              if (null!=chosenContactId)
              {
                 Contact cont=new Contact(Id=chosenContactId);
                  delete cont;
                  //if(Test.isRunningTest())
                  //    Integer intTest =20/0;
            
                  contacts=null;
                  chosenContactId=null;
              }
           }
        }
     
        public List<Contact> getContacts()
        {
           if ( (null!=getAccount().id) && (contacts == null) )
           {
               contacts=[SELECT Id, FirstName, LastName, Name, Title, Email, CleanStatus, Active__c, AccountId, Phone, CreatedDate,
                        MailingStreet, MailingCity, MailingState, MailingPostalCode, Contact_Status_Tracking__c, Agent_CICA_ID__c, Agent_FSL_ID__c
                        FROM Contact
                        WHERE AccountId = : getAccount().ID
                        ORDER BY CreatedDate]; 
           }
                               
           return contacts;
        }
    
    //Account Contact Roles
 
        public List<AccountContactRole> contactRoles;
        public List<AccountContactRole> getContactRoles(){
            if((getAccount().id != null) && (contactRoles == null)){
                contactRoles = [SELECT Id, Role, IsPrimary, AccountId, Contact.Name 
                                FROM AccountContactRole
                                WHERE AccountId = : getAccount().ID
                                ORDER BY CreatedDate];         
            }
            return contactRoles;
        }
        //will hold the AccountContactRole records to be saved
        public List<AccountContactRole> lstRole  = new List<AccountContactRole>();
        public List<Contact> lstCtc = new List<Contact>();
    
        //list of the inner class
        public List<innerClass> lstInner {get;set;}
        public List<innerClass> lstInner2 {get;set;}
    
        //will indicate the row to be deleted
        public String selectedRowIndex {get;set;}  
    
        //no. of rows added/records in the inner class list
        public Integer count = 1;
        //{get;set;}
        
    
        //save the records by adding the elements in the inner class list to lstRole,return to the same page
      /*  public Boolean UpdateRole(){
             Boolean newRole;
          //    Boolean result;
          //  PageReference pr=ApexPages.currentPage();
            
            for(Integer j = 0;j<lstInner.size();j++)
            {
                lstRole.add(lstInner[j].contactRole);
            } 
            insert lstRole;
        
            return newRole = True;
        }
    */
        public PageReference save()
        {
            Boolean result=true;
          //  Boolean newRole=true;
          //  Boolean result1=true;
            PageReference pr=ApexPages.currentPage();

            if (null!=getAccount().id)
            {
            result=updateContacts();
        //    newRole=updateRole();
              //if (null!=getAccount().id){
              //  result1=updateContacts();
              //  }
            }
            else
            {
            pr.setRedirect(true);
            }

            if (result==true)
            {
            // call standard controller save, but don't capture the return value which will redirect to view page
            std.save();
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
            }
            ApexPages.currentPage().getParameters().put('id', getAccount().id);

            return pr;
            //  system.debug('pr:----->' +pr);

        }
        
        //add one more row
       public void Add()
        {   
            count = count+1;
            ShowAddRoles = True;
            addMore(); 
            addMore2();     
        }
        
        public void Add2()
        {   
            count = count+1;
            ShowAddRoles = True;
           // addMore(); 
            addMore2();     
        }
    
        /*Begin addMore*/
        public void addMore(){
            ShowAddRoles = True;  
            //call to the iner class constructor
            innerClass objInnerClass = new innerClass(count, currentAccount);
         //   innerClass objInnerClass2 = new innerClass(count, currentAccount);
            
            //add the record to the inner class list
            lstInner.add(objInnerClass);
        //    lstInner2.add(objInnerClass2);      
            system.debug('lstInner---->'+lstInner);
                     
        }/* end addMore*/
        
        public void addMore2(){
            ShowAddRoles = True;  
            //call to the iner class constructor
        //    innerClass objInnerClass = new innerClass(count, currentAccount);
            innerClass objInnerClass2 = new innerClass(count, currentAccount);
            
            //add the record to the inner class list
        //    lstInner.add(objInnerClass);
            lstInner2.add(objInnerClass2);      
            system.debug('lstInner---->'+lstInner2);
                     
        }/* end addMore*/
        
        public void Del()
        {
            system.debug('selected row index---->'+selectedRowIndex);
            lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
          //  lstInner2.remove(Integer.valueOf(selectedRowIndex)-1);
            count = count - 1;
            ShowAddRoles = False;
            
        }
        
        public PageReference SaveRole(){
        PageReference pr = new PageReference('/apex/GroupAccountGrid');
     //    PageReference pr=ApexPages.currentPage();
        
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstRole.add(lstInner[j].contactRole);
        } 
        insert lstRole;
        pr.setRedirect(True);
        return pr;
        }
        
        public PageReference SaveContact(){
        PageReference pr = new PageReference('/apex/GroupAccountGrid');
        
        for(Integer j = 0;j<lstInner2.size();j++)
        {
            lstCtc.add(lstInner2[j].newContact);
        } 
        insert lstCtc;
        pr.setRedirect(True);
        return pr;
        }
        
     //   public String recCount  {get;set;}
        
        
        public AccountContactRole contactRole   {get;set;}
       
        @TestVisible
        public class innerClass{    
            public String recCount{get;set;}
            public AccountContactRole contactRole   {get;set;}
            public Contact newContact {get;set;}
            public innerClass(Integer intCount , Account acc){
                //   Contact ctc;
                   recCount = String.valueOf(intCount); 
                   if(acc != null && acc.Id != null ){      
                   System.Debug('#######current Account Id:' + acc.Id);
                   newContact = new Contact(AccountId=acc.Id);
                 //  contactRole = new AccountContactRole(AccountId=acc.Id);                   
                   }
          /*   public innerClass(Integer intCount , Contact ctc){
                   recCount = String.valueOf(intCount); 
                   if(ctc != null && ctc.Id != null ){      
                //   System.Debug('#######current Account Id:' + acc.Id);
                 //  newContact = new Contact(AccountId=acc.Id);
                   contactRole = new AccountContactRole(AccountId=ctc.Account.Id, ContactId=ctc.Id);                   
                   }       */
            }
        }/*End inner Class*/
}
 
@isTest(SeeAllData=true)
private without sharing class Sept27Test {

// Update Account, Contact and Contact Roles on Plan
    // Update Contacts on Account
    static testMethod void updateAcctAndContacts() {
     
       // insert tax id
       Tax_Id__c t = new Tax_Id__c();
       t.Name = '912884025';
       insert t; 
        
        // insert client account
        Account ca = new Account();
        ca.name = 'Client Test Account';
        ca.RecordTypeId = '012800000003UY8AAM'; 
        ca.BillingState = 'OH';
        ca.BillingPostalCode = '44241';
        ca.Tax_ID__c = t.Id;
        insert ca;
        
        // insert broker account
        Account bor = new Account();
        bor.name = 'Broker Test Account';
        bor.RecordTypeId = '012800000003UYI'; 
        bor.BillingState = 'OH';
        bor.BillingPostalCode = '44241';
        bor.Tax_ID__c = t.Id;
        insert bor;
          
        test.starttest();
       
            ApexPages.StandardController std1 = new ApexPages.StandardController(ca);
            AccountAndContactsGridExtension objAct=new AccountAndContactsGridExtension(std1);
            AccountAndContactsGridExtension ShowAddRoles = new AccountAndContactsGridExtension();
            Contact objCtc = new Contact(MailingState='OH', firstName='newContactFirstName', LastName='Doe', AccountId=objAct.getAccount().Id);
            //Id objCtId=[SELECT Id FROM Contact WHERE ];
            insert objCtc;
            AccountContactRole xRole= new AccountContactRole(AccountId=objAct.getAccount().Id, ContactId=objCtc.Id, Role='The Tester Pro', IsPrimary=False);
            System.debug('Contact created---->   '+objCtc.Id);
         //   System.debug(objCtc.Id);
           
          
        
       //     ApexPages.Pagereference result = objAct.updateContacts();
            // insert contact
            Contact testContact = new Contact();
            testContact.FirstName = 'Tester';
            testContact.LastName = 'testContact';
            testContact.AccountID = bor.id;
            testContact.Email = 'test@testcontact.com';
            testContact.Active__c = true;
            testContact.MailingState = 'OH';
            insert testContact;
            testContact.FirstName = 'Tester woman';
            update testContact;
            
            objAct.newContactFirstName = testContact.FirstName;
            objAct.newContactLastName = testContact.LastName;
            objAct.chosenContactId = testContact.Id;
            
              
            AccountContactRole objRole = new AccountContactRole(ContactId=testContact.Id, AccountId=objAct.getAccount().Id, Role='Tester', IsPrimary=False);
            insert objRole;
            objRole.Role = 'tester admin';
            update objRole;
        
            //  delete testContact;
            Contact cont=new Contact(Id=objAct.chosenContactId);
            update cont;
            delete cont;
        
            Contact testContact2 = new Contact();
            testContact2.FirstName = 'Testing';
            testContact2.LastName = 'testingContact';
            testContact2.AccountID = bor.id;
            testContact2.Email = 'test2@testcontact.com';
            testContact2.Active__c = true;
            insert testContact2;
            // delete testContact2;
        
            // New Contact Roles
            AccountContactRole testRole = new  AccountContactRole();
            testRole.ContactId=testContact2.Id;
            testRole.AccountId=ca.Id;
            testRole.Role='Tester';
            testRole.IsPrimary=True;
            insert testRole;
             
            AccountContactRole testRole2 = new  AccountContactRole();
            testRole2.ContactId=testContact2.Id;
            testRole2.AccountId=ca.Id;
            testRole2.Role='Tester2';
            testRole2.IsPrimary=False;
            insert testRole2;  
         
       //  objAct.contactRole = testRole;      
                 
            objAct.getSobject();
            objAct.getAccount();
            objAct.DoProcess();
            ObjAct.SaveAndExit();
         
            
            objAct.getAccount();
            objAct.getContacts();
          
          //  objAct.updateContacts();
            objAct.SaveContact();
            system.debug('Account---->'+objAct);
            system.debug('Contact----->'+objAct.getContacts());
            objAct.DeleteContact();
            ObjAct.Save();
         
            objAct.getContactRoles();
            system.debug('Role-------->'+xRole);
            objAct.SaveRole();
           
            objAct.Del();
          
        
            objAct.SaveRole();
           std1.Save();
           List<Apexpages.Message> msgs = ApexPages.getMessages();
            boolean b = false;
            for(Apexpages.Message msg:msgs){
                if (msg.getDetail().contains('Changes saved')) b = true;
            }
            system.assert(b);
            ObjAct.SaveAndExit();                    
       
        test.stoptest();
     }
 }

The code coverage is at 70%.

Thank you for your help.
Bryan Leaman 6Bryan Leaman 6
You say the test is failing. Please provide the error message you're receiving.

If you're just wanting to increase coverage, then use Developer Console to run the test and after it completes, double-click the class in the bottom-right window that shows 70% coverage and it'll highlight lines in red if they're not being executed.
Mamadou Diallo 16Mamadou Diallo 16
Here is the error message:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ContactId]: [ContactId]

Stact Trace:
Class.AccountAndContactsGridExtension.SaveRole: line 269, column 1
Class.BotPlanAuditTest.AccountAndContactsGridExtension: line 132, column 1

Thank you for your help.