+ Start a Discussion
Mohammed Ikram 7Mohammed Ikram 7 

I have controller which returns the following

public class MixedObjectVFController {

    public static List<List<String>> getObjectList(){
    List<List<String>> strList = new List<List<String>>();
    List<Account> acc = [select name,phone,AccountNumber from account limit 100];

    for(account a : acc){
        List<String> tempList = new List<String>();
        tempList.add('account');
        tempList.add(a.name);
        tempList.add(a.phone);
         tempList.add(a.AccountNumber );
        strList.add(tempList);
    }
    List<contact> cList = [select name,phone,email from contact limit 100];
    for(contact a : cList){
        List<String> tempList = new List<String>();
        tempList.add('contact');
        tempList.add(a.name);
        tempList.add(a.phone);
         tempList.add(a.email );
        strList.add(tempList);
    }
    return strList;
}
}
Best Answer chosen by Mohammed Ikram 7
Ajay K DubediAjay K Dubedi
Hi Mohammed,

Your problem has solved Please try this code.
you can use where condition in your query only for not required field.

<apex:page controller="MixedObjectVFController" >
<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:pageBlockTable value="{!objectList}" var="item">
            <apex:column headerValue="Object" value="{!item[0]}" />
            <apex:column headerValue="Name" value="{!item[1]}" />
            <apex:column headerValue="Phone" value="{!item[2]}" />
            <apex:column headerValue="Email" value="{!item[3]}" />
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:page>

public class MixedObjectVFController {
    
    public static List<List<String>> getObjectList(){
        List<List<String>> strList = new List<List<String>>();
        List<Account> acc = [select name,phone,AccountNumber from account Where Phone!=Null AND AccountNumber!=Null limit 100];
        
        for(account a : acc){
            List<String> tempList = new List<String>();
            tempList.add('account');
            tempList.add(a.name);
            tempList.add(a.Phone);
            tempList.add(a.AccountNumber );
            strList.add(tempList);
        }
        List<contact> cList = [select name,phone,email from contact Where Phone!=Null AND Email!=Null limit 100];
        for(contact a : cList){
            List<String> tempList = new List<String>();
            tempList.add('contact');
            tempList.add(a.name);
            tempList.add(a.Phone);
            tempList.add(a.email );
            strList.add(tempList);
        }
        return strList;
    }
}

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi

All Answers

Mohammed Ikram 7Mohammed Ikram 7
I have a VF page 
<apex:page controller="MixedObjectVFController" >
<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:pageBlockTable value="{!objectList}" var="item">
            <apex:column headerValue="Object" value="{!item[0]}" />
            <apex:column headerValue="Name" value="{!item[1]}" />
     
          
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:page>

if I want to display   tempList.add(a.phone); , What should be in the VF page , I tried
     <apex:column headerValue="Name" value="{!item[2]}" />

THe following messagwe is displayed
Subscript value 2 not valid. Must be between 0 and 1  

Please Help.
Ikram
Diwakar GDiwakar G
You are getting error because phone number is not mandatory while creating a record. A user may enter or the field can be leaved as blank. A list contains both phone number and null values. The compiler cannot fetch null values. Instead of phone number you can give Id and check.

public class MixedObjectVFController {

    public static List<List<String>> getObjectList(){
    List<List<String>> strList = new List<List<String>>();
    List<Account> acc = [select name,Id from account limit 100];

    for(account a : acc){
        List<String> tempList = new List<String>();
        tempList.add('account');
        tempList.add(a.name);
        tempList.add(a.Id);
        strList.add(tempList);
    }
    List<contact> cList = [select name,Id from contact limit 100];
    for(contact a : cList){
        List<String> tempList = new List<String>();
        tempList.add('contact');
        tempList.add(a.name);
        tempList.add(a.Id);
        strList.add(tempList);
    }
    return strList;
}
}


<apex:page controller="MixedObjectVFController" >
<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:pageBlockTable value="{!objectList}" var="item">
            <apex:column headerValue="Object" value="{!item[0]}" />
            <apex:column headerValue="Name" value="{!item[1]}" />
             <apex:column headerValue="Id" value="{!item[2]}" />
          
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:page>




Mark solved if it does help you.
Ajay K DubediAjay K Dubedi
Hi Mohammed,

Your problem has solved Please try this code.
you can use where condition in your query only for not required field.

<apex:page controller="MixedObjectVFController" >
<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:pageBlockTable value="{!objectList}" var="item">
            <apex:column headerValue="Object" value="{!item[0]}" />
            <apex:column headerValue="Name" value="{!item[1]}" />
            <apex:column headerValue="Phone" value="{!item[2]}" />
            <apex:column headerValue="Email" value="{!item[3]}" />
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:page>

public class MixedObjectVFController {
    
    public static List<List<String>> getObjectList(){
        List<List<String>> strList = new List<List<String>>();
        List<Account> acc = [select name,phone,AccountNumber from account Where Phone!=Null AND AccountNumber!=Null limit 100];
        
        for(account a : acc){
            List<String> tempList = new List<String>();
            tempList.add('account');
            tempList.add(a.name);
            tempList.add(a.Phone);
            tempList.add(a.AccountNumber );
            strList.add(tempList);
        }
        List<contact> cList = [select name,phone,email from contact Where Phone!=Null AND Email!=Null limit 100];
        for(contact a : cList){
            List<String> tempList = new List<String>();
            tempList.add('contact');
            tempList.add(a.name);
            tempList.add(a.Phone);
            tempList.add(a.email );
            strList.add(tempList);
        }
        return strList;
    }
}

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
This was selected as the best answer
Mohammed Ikram 7Mohammed Ikram 7
Thanks a lot Ajay,

it worked.

Ikram