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
Giri Kumar BGiri Kumar B 

I am writting wrapper class display record

I have a requirement to display the record account and contact .  i am writing a wrapper class in account and contact to display record

 my class
   
    public  class conacc {
        public List<MyWrapper> wrapper {get; set;}
      public List<Account> accLst1 {get; set;}
        public List<Contact> conLst1 {get; set;}
        public conacc ()
        {
          accLst1 = [select id,Name from Account  ] ;
           conLst1 = [select Id,Name from contact where AccountId IN : accLst1 ] ;
           wrapper = new List<MyWrapper>() ;
           for(Integer i=0 ; i < 20; i++)
                wrapper.add(new MyWrapper(  conLst1, accLst1[i])) ;
        
        }
    public class MyWrapper
        {
            public Account accRec1 {get; set;}
            public List<Contact> conRec1 {get; set;}
            public MyWrapper( List<Contact> con ,Account acc )
            {
                accRec1 = acc ;
                conRec1 = con ;
            }
    }
    }
    
    i am try to write the test class in wrapper class hit error 
    test class
    
    @istest
    public class wraptest {
    @istest
        public static void mains(){
    /* i am try to record are insert but they are hit error Constructor not defined 
     Which record i am insert for exp:
    account a = new account();
    a.name = "test1";
    insert a;`enter code here`
    i am not understand how to write this wrapper test class
    */
         conacc.MyWrapper  con = new conacc.MyWrapper();
    
    }
    }

 
Deepak Pandey 13Deepak Pandey 13
@istest(seealldata=true)
public class testclass
{
        public static void mains(){:
    account a = new account();
    a.name = "test1";
    insert a;
   
   contact objCon = new Contact();
  con.lastname = 'TestCon';
  con.Accountid = a.id';
  insert con;
conacc.MyWrapper wraptst =  new conacc.MyWrapper(con);
}
}
 
Giri Kumar BGiri Kumar B
Thank you for replay Deepak . hit error same : Constructor not defined: [conacc.MyWrapper].<Constructor>(Contact);
User-added image
Deepak Pandey 13Deepak Pandey 13
  public Account accRec1 {get; set;}
            public List<Contact> conRec1 {get; set;}
            public MyWrapper( List<Contact> con ,Account acc )
            {
                accRec1 = acc ;
                conRec1 = con ;
            }



this is not useable code friend. 
Read this article -  http://www.infallibletechie.com/2015/03/sample-wrapper-class-using-apex-in.html
 
Ramssf70Ramssf70
Hi Giri,
you can try bellow code it is working
@istest
public class aaatestclass
{
      static testmethod void mytest()
        {
      
        account a = new account();
        a.name = 'test1';
        insert a;
               
        List<contact> conlist = new List<contact>();
        contact objCon = new Contact();
        objCon.lastname = 'TestCon';
        objCon.Accountid = a.id;
        conlist.add(objCon);
        
        contact objCon1 = new Contact();
        objCon1.lastname = 'TestCon';
        objCon1.Accountid = a.id;
        conlist.add(objCon1);
        
        insert conlist;
       
        //conacc obj = new conacc();
        conacc.MyWrapper wraptst =  new conacc.MyWrapper(conlist,a);
        }
}
Giri Kumar BGiri Kumar B
Thank you respond my quation Ramssf70 yes it working . but they are showing 35% only how to increage the percentage Ramssf70
User-added image
Giri Kumar BGiri Kumar B
can you please update me Ramssf70
Giri Kumar BGiri Kumar B
Yes i got 100 percent Test Class . Thank you Ramssf70
@istest
public class contestclass
{
      static testmethod void mytest()
        {
      
            list<account> m = new list<account>();
             account md = new account();
              md.name = 'test3';
              m.add(md);
            account a = new account();
              a.name = 'test1';
               m.add(a);
                insert m;
                                
        List<contact> conlist = new List<contact>();
        contact objCon = new Contact();
        objCon.lastname = 'TestCon';
        objCon.Accountid = a.id;
        conlist.add(objCon);
        
        contact objCon1 = new Contact();
        objCon1.lastname = 'TestCon';
        objCon1.Accountid = a.id;
        conlist.add(objCon1);
        
        insert conlist;
       
      //conacc obj = new conacc();
     //  list<account>  mo = [select id,name from account];
       //   list<contact> con = [select id,name from contact where account id = '']
        conacc.MyWrapper wraptst =  new conacc.MyWrapper(conlist,a);
}
}
User-added image
Ramssf70Ramssf70
Hi,
If your problem has solved mark as a best answer.
Thank you