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
Ap30Ap30 

wrapper class to display two related custom object records in a single table

Hello All,
I'm getting below error in my Apex. Please guide me.
Error: Compile Error: Constructor not defined: [TrainingDataWrapperController.wrapRecord].<Constructor>(Trainers_Detail__c) at line 13 column 36

line 13 ---> wrapRecordList.add(new wrapRecord(t));

================code===============

public class TrainingDataWrapperController {
 
    //Our collection of the class/wrapper objects wrapRecord 
    public List<wrapRecord> wrapRecordList{get; set;}
    public list<custobj1> obj1{get;set;}    
    public list<custobj2> obj2{get;set;} 
     
    public TrainingDataWrapperController (){
        if(wrapRecordList == null) {
            wrapRecordList = new List<wrapRecord>();
            for (custobj1 t: [Select Id, First_Name__c, Last_Name__c, Education__c, Technology__c ,(Select Name From custobj2) From custobj1 Limit 1]){
                // As each detail is processed we create a new wrapRecord object and add it to the wrapRecordList
                wrapRecordList.add(new wrapRecord(t));
            }
        }
    }
 
   
 
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. 
    public class wrapRecord {
        public custobj1 td {get; set;}
        public custobj2 tdeal {get; set;}
 
        //This is the contructor method. When we create a new wrapRecord object.
        public wrapRecord (custobj1 tds,custobj2 tdeals) {
            td = tds;
            tdeal = tdeals;
        }
    }
}
Best Answer chosen by Ap30
Suraj Tripathi 47Suraj Tripathi 47
Hi Ap30,

Please check this code:-
public class TrainingDataWrapperController {
 
    //Our collection of the class/wrapper objects wrapRecord 
    public List<wrapRecord> wrapRecordList{get; set;}
    public list<custobj1> obj1{get;set;}    
    public list<custobj2> obj2{get;set;} 
     
    public TrainingDataWrapperController (){
        if(wrapRecordList == null) {
            wrapRecordList = new List<wrapRecord>();
            for (custobj1 t: [Select Id, First_Name__c, Last_Name__c, Education__c, Technology__c ,(Select Name From custobj2) From custobj1]){
                // As each detail is processed we create a new wrapRecord object and add it to the wrapRecordList
                if(!t.custobj2.isEmpty()){
                   for(custobj2 t1: t.custobj2){
                        wrapRecordList.add(new wrapRecord(t, t1));
                   }
                }             
            }
        }
    }
 
   
 
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. 
    public class wrapRecord {
        public custobj1 td {get; set;}
        public custobj2 tdeal {get; set;}
 
        //This is the contructor method. When we create a new wrapRecord object.
        public wrapRecord (custobj1 tds,custobj2 tdeals) {
            td = tds;
            tdeal = tdeals;
        }
    }
}
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Ap30,

You are getting an error because Constructor is taking 2 arguments but you have passed only 1 argument.
User-added image
Here you have given only one argument in wrapRecord.

User-added image
But here wrapRecord is taking 2 arguments

You can take reference from this below code.
public class TrainingDataWrapperController {
 
    //Our collection of the class/wrapper objects wrapRecord 
    public List<wrapRecord> wrapRecordList{get; set;}
    public list<custobj1> obj1{get;set;}    
    public list<custobj2> obj2{get;set;} 
     
    public TrainingDataWrapperController (){
        if(wrapRecordList == null) {
            wrapRecordList = new List<wrapRecord>();
            for (custobj1 t: [Select Id, First_Name__c, Last_Name__c, Education__c, Technology__c ,(Select Name From custobj2) From custobj1 Limit 1]){
                // As each detail is processed we create a new wrapRecord object and add it to the wrapRecordList
                wrapRecordList.add(new wrapRecord(t, t.custobj2[0]));
            }
        }
    }
 
   
 
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. 
    public class wrapRecord {
        public custobj1 td {get; set;}
        public custobj2 tdeal {get; set;}
 
        //This is the contructor method. When we create a new wrapRecord object.
        public wrapRecord (custobj1 tds,custobj2 tdeals) {
            td = tds;
            tdeal = tdeals;
        }
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
Ap30Ap30
Thanks. The above code is working. But it displays only one record. What changes need to be done if we want to display all the records.
Ap30Ap30
It's throwing error in visualforce page ,
List index out of bounds: 0 An unexpected error has occurred. Your development organization has been notified.
Suraj Tripathi 47Suraj Tripathi 47
Hi Ap30,

Please check this code:-
public class TrainingDataWrapperController {
 
    //Our collection of the class/wrapper objects wrapRecord 
    public List<wrapRecord> wrapRecordList{get; set;}
    public list<custobj1> obj1{get;set;}    
    public list<custobj2> obj2{get;set;} 
     
    public TrainingDataWrapperController (){
        if(wrapRecordList == null) {
            wrapRecordList = new List<wrapRecord>();
            for (custobj1 t: [Select Id, First_Name__c, Last_Name__c, Education__c, Technology__c ,(Select Name From custobj2) From custobj1]){
                // As each detail is processed we create a new wrapRecord object and add it to the wrapRecordList
                if(!t.custobj2.isEmpty()){
                   for(custobj2 t1: t.custobj2){
                        wrapRecordList.add(new wrapRecord(t, t1));
                   }
                }             
            }
        }
    }
 
   
 
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. 
    public class wrapRecord {
        public custobj1 td {get; set;}
        public custobj2 tdeal {get; set;}
 
        //This is the contructor method. When we create a new wrapRecord object.
        public wrapRecord (custobj1 tds,custobj2 tdeals) {
            td = tds;
            tdeal = tdeals;
        }
    }
}
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
 
This was selected as the best answer
Diana S N 4Diana S N 4
Iam  getting so many errors but  I want to display the records of 2 custom objects in VFpage using wrapper class 
objects Trainingdeal__c and Trainer__c from TrainingDeal__c object want only name and from Trainer__c object want yo display Firstname,lastname,education, technology  

public class wrapperdemo{
    public list<wrapRecord> wrapRecordlist{get;set;}
    public list<TrainingDeal__c> obj1{get;set;}
    public  list<Trainer__c> obj2{get;set;}
    public wrapperdemo(){
        if(wrapRecordlist==null){
            wrapRecordlist=new  list<wrapRecord>();
            for(TrainingDeal__c t:[select Name(select FirstName__c, LastName__c, Education__c from Trainer__c) from TrainingDeal__c]){
                if(!.Trainer__c.isEmpty()){
                    for(Trainer__c t1 :Trainer__c){
                        wrapRecordlist.add(new wrapRecord(t,t1));
                    }
                }            }
        }
    }

    public class wrapperdemo{
        public TrainingDeal__c td{get;set;}
        public Trainer__c tdeal{get;set;}
        
        public wrapRecord(TrainingDeal__c tds,Trainer__c tdeas){
            td=tds;
            tdeal=tdeals;
        }
    }
}