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
ravi kiran 87ravi kiran 87 

Create multiple columns on VF page based on different queries.

I have a requirement where I have to display mutliple columns from different objects on a single VF page

i'm trying to display but the way its binding is improper.

public class YourController {

    public String GetAttributes { get; set; }
public List<Vehicle> vehicles { get; set; }

    public YourController() {

        vehicles = new List<Vehicle>();

        List<account> cars = [SELECT Id, Name, Description FROM account];
        
        List<contact> planes = [SELECT Id, name , Title FROM contact];

        for(account car : cars) {
        system.debug('car value in the loop ---> '+car.name);
            vehicles.add(new Vehicle(car));
        }

        for(contact plane : planes) {
        system.debug('Plane value in the loop ---> '+plane.title);
            vehicles.add(new Vehicle(plane));
        }
        
    }
    
    
      public List<contact> GetAttributes(){
    List<contact> attr = [SELECT Name from contact limit 10];
    return attr;
    }
    

    public class Vehicle {
        public Id id { get; set; }
        public string name { get; set; }
        public string name1{ get; set; }

        public Vehicle(account car) {
     //       this.id = car.Id;
            this.name = car.name;
           // this.desc = car.desc;
        }

        public Vehicle(contact plane) {
       //     this.id = plane.Id;
            this.name1 = plane.title;
            //this.desc = plane.desc;
        }
    }
}


VF page code 


<apex:page controller="YourController" >
<apex:form >
<apex:pageBlock ><apex:pageBlockTable value="{!vehicles}" var="vehicle">
   <!--- <apex:column headerValue="Id" value="{!vehicle.id}" />  --->
    <apex:column headerValue="FirstName" value="{!vehicle.name}" />
    <apex:column headerValue="Name" value="{!vehicle.name1}" />
    
    <apex:pageBlockSection title="Vitality Check Attributes">
        <apex:pageBlock >
            <apex:repeat value="{!Attributes}" var="att">
            </apex:repeat>
        </apex:pageBlock>
    </apex:pageBlockSection>
    
    
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>



 
ravi kiran 87ravi kiran 87
public class YourController {

    public String GetAttributes { get; set; }
    public List<Vehicle> vehicles { get; set; }

    public YourController() {

        vehicles = new List<Vehicle>();

        List<account> cars = [SELECT Id, Name, Description FROM account];
        
        List<contact> planes = [SELECT Id, name , Title FROM contact];

        for(account car : cars) {
        system.debug('car value in the loop ---> '+car.name);
            vehicles.add(new Vehicle(car));
        }

        for(contact plane : planes) {
        system.debug('Plane value in the loop ---> '+plane.title);
            vehicles.add(new Vehicle(plane));
        }
        
    }
    
    
      public List<contact> GetAttributes(){
    List<contact> attr = [SELECT Name from contact limit 10];
    return attr;
    }
    

    public class Vehicle {
        public Id id { get; set; }
        public string name { get; set; }
        public string name1{ get; set; }

        public Vehicle(account car) {
     //       this.id = car.Id;
            this.name = car.name;
           // this.desc = car.desc;
        }

        public Vehicle(contact plane) {
       //     this.id = plane.Id;
            this.name1 = plane.title;
            //this.desc = plane.desc;
        }
    }
}


VF page code 


<apex:page controller="YourController" >
<apex:form >
<apex:pageBlock ><apex:pageBlockTable value="{!vehicles}" var="vehicle">
   <!--- <apex:column headerValue="Id" value="{!vehicle.id}" />  --->
    <apex:column headerValue="FirstName" value="{!vehicle.name}" />
    <apex:column headerValue="Name" value="{!vehicle.name1}" />
    
    <apex:pageBlockSection title="Vitality Check Attributes">
        <apex:pageBlock >
            <apex:repeat value="{!Attributes}" var="att">
            </apex:repeat>
        </apex:pageBlock>
    </apex:pageBlockSection>
    
    
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 
SaurabhGupta_SaurabhGupta_
Create a wrapper class with all your required fields and then create a list of class and use it VF page table.
ravi kiran 87ravi kiran 87
I tried the same but still its not working.

Here Below is the code 
public class YourController {

  

public List<Vehicle> vehicles { get; set; }

    public YourController() {

        vehicles = new List<Vehicle>();

        List<account> cars = [SELECT Id, Name, Description FROM account];
        
        List<contact> planes = [SELECT Id, name , Title FROM contact];

        for(account car : cars) {
        system.debug('car value in the loop ---> '+car.name);
            vehicles.add(new Vehicle(car));
        }

        for(contact plane : planes) {
        system.debug('Plane value in the loop ---> '+plane.title);
            vehicles.add(new Vehicle(plane));
        }
    }

    public class Vehicle {
        public Id id { get; set; }
        public string name { get; set; }
        public string name1{ get; set; }

        public Vehicle(account car) {
            this.id = car.Id;
            this.name = car.name;
           // this.desc = car.desc;
        }

        public Vehicle(contact plane) {
            this.id = plane.Id;
            this.name1 = plane.title;
            //this.desc = plane.desc;
        }
    }
}

----------------------------------------------

<apex:page controller="YourController" >
<apex:form >
<apex:pageBlock ><apex:pageBlockTable value="{!vehicles}" var="vehicle">
    <apex:column headerValue="Id" value="{!vehicle.id}" />
    <apex:column headerValue="FirstName" value="{!vehicle.name}" />
    <apex:column headerValue="Name" value="{!vehicle.name1}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>