Creating list from custom objects with relationship [closed]

I want to create a list in order to use it in a vf page with the tag apex:pageblocktable.
The schema below is just for learning purposes, it has 3 custom objects with look up relationships.

Schema

I had create the following queries but I think is far way from been efficient, specially the section were I am bringing the brand name as I loop through all the rows until i find an Id match.

    string brandName;
string carType;    
list<list<string>> Result = new list<list<string>>();
public list<list<string>> getResult(){
    list<Model__c> cars1 = [SELECT Name, Brand__c, (Select Name FROM cars1__r) FROM Model__c];
    list<Brand__c> brand = [SELECT Name,Country__c FROM Brand__c];
    for (Model__c c:cars1 ){
        for(Car__c details:c.cars1__r){
            carType = details.name;
            //GET THE BRAND OF THE CAR
            for(Brand__c b:brand){
                if (c.Brand__c == b.Id){
                    brandName = b.name;
                }
            }
            //system.debug(brandName + ' ' + c.Name + ' ' + carType);
            list<string> subresult = new List<string>();
            subresult.add(brandName);
            subresult.add(c.Name);
            subresult.add(carType);
            result.add(subresult);
        }
    }     
    return Result;
}

Also the problem is when I had my list, that is a group of lists inside the list I havent been able to find in the docs how to name each list, in order to use the apex tags to display in the vf page.

Answer

Your solution is almost ok, but it’s better to have a inner class to maintain lists like this. Then we can maintain any kind of data structure that we need to have in client side(VF page) instead of having list of list, take the benefit of list collection with your own structure(below is a how to, but not tested).

public with sharing class myController{
public List<UIWrapper> uiWrapperList{get;set;}

    public List<UIWrapper> getuiWrapperList(){
    uiWrapperList = new List<UIWrapper>();
    //your data retrieving goes here
    list<Model__c> cars1 = [SELECT Name, Brand__c, (Select Name FROM cars1__r) FROM Model__c];
        list<Brand__c> brand = [SELECT Name,Country__c FROM Brand__c];
        for (Model__c c:cars1 ){
            for(Car__c details:c.cars1__r){
                carType = details.name;
                //GET THE BRAND OF THE CAR
                for(Brand__c b:brand){
                    if (c.Brand__c == b.Id){
                        brandName = b.name;
                    }
                }

                UIWrapper wrapper = new UIWrapper();
                wrapper.setBrandName(brandName);
                wrapper.setCarName(c.Name);
                wrapper.setCarType(carType);
                uiWrapperList.add(wrapper);
            }
        }    
    }

    public class UIWrapper{
        public string brandName{get;set;}
        public string carName{get;set;}
        public string carType{get;set;}
    }
}

Then in your VF page, simply use the wrapper list as follows

<apex:pageBlockTable value="{!uiWrapperList}" var="item">
    <apex:column value="{!item.brandName}"/>
    <apex:column value="{!item.modelName}"/>
    <apex:column value="{!item.carName}"/>
</apex:pageBlockTable>

Attribution
Source : Link , Question Author : manza , Answer Author : highfive

Leave a Comment