+ Start a Discussion
mukesh guptamukesh gupta 

Related list By SOQL

Hi Expert,

I want to get Account realted list that would be display in Visualforce page. so i am using below query. but when i run the code and store each object value in list then get error "List index  is outbound"

public List<Account> accList{get;set;}
    public List<Opportunity> oppList{get;set;}
    public List<Contact> contList{get;set;}


     List<Account> acc= [SELECT Name, (SELECT Name, Type FROM Opportunities),(SELECT Name, email FROM Contacts) FROM Account where Name = 'Dickenson plc'];
    
     accList = acc;
     contList = new List<Contact>(acc[0].Contacts);
     oppList = new List<Opportunity>(acc[0].Opportunities);


Please suggest

Thanks
Mukesh
 
Best Answer chosen by mukesh gupta
Amit Singh 1Amit Singh 1
May I suggest to replace String searchItem = '*'+selectedValue+'*'; Line with String searchItem = selectedValue; Line and make sure you have account with the Selected Name.

Thanks!

All Answers

Amit Singh 1Amit Singh 1
Use below code,
 
public List<Account> accList{get;set;}
    public List<Opportunity> oppList{get;set;}
    public List<Contact> contList{get;set;}


     List<Account> acc= [SELECT Name, (SELECT Name, Type FROM Opportunities),(SELECT Name, email FROM Contacts) FROM Account where Name = 'Dickenson plc'];
    
     accList = acc;
	 If(accList!=null && accList.size()>0){
		 contList = new List<Contact>(acc[0].Contacts);
		 oppList = new List<Opportunity>(acc[0].Opportunities);
    }

Let me know if this helps :)

Thanks!
mukesh guptamukesh gupta
Hi Amit,

I am using below code. but not getting list on VF page. This is Apex controller
 
public with sharing class AssociateController {

    public String selectedValue { get; set; }
    public List<String> strSearch { get; set; }  
    public List<Account> accList{get;set;}
    public List<Opportunity> oppList{get;set;}
    public List<Contact> contList{get;set;}
    
    public PageReference searchMethod() {
        
    accList = new List<Account>();
    oppList = new List<Opportunity>();
    contList = new List<Contact>();
    
    String searchItem = '*'+selectedValue+'*';
    
  
     List<Account> acc= [SELECT Name, (SELECT Name, Type FROM Opportunities),(SELECT Name, email FROM Contacts) FROM Account where Name = :searchItem];
    
   
    accList = acc;

     If(accList!=null && accList.size()>0){

         contList = new List<Contact>(acc[0].Contacts);

         oppList = new List<Opportunity>(acc[0].Opportunities);

    }
    return null; 
    }
    
    public AssociateController(){
    strSearch = new List<String>();
    
    for(Account a: [SELECT id, Name From Account]){
        strSearch.add(a.name);
    }
    
    
    }

    
}

and this is VF page 
 
<apex:page controller="AssociateController" docType="html-5.0">
<apex:form >
    <apex:inputText list="{!strSearch}" value="{!selectedValue}"/>
    <apex:commandButton value="Search Lead and Contact" action="{!searchMethod}" reRender="acct,cont,opprty" status="actStatus"/>
    
    <apex:actionStatus id="actStatus">
        <apex:facet name="start">
             <img src="/img/loading.gif"/>
        </apex:facet>
    </apex:actionStatus>
    
    <apex:pageBlock title="Account List" id="acct">
        <apex:pageBlockTable value="{!accList}" var="acc">
            <apex:column value="{!acc.Name}"/>
            <apex:column value="{!acc.Type}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    
    <apex:pageBlock title="Contacts" id="cont">
        <apex:pageblockTable value="{!contList}" var="con">
          <apex:column value="{!con.name}"/>
          <apex:column value="{!con.email}"/>
     </apex:pageblockTable>
    </apex:pageBlock>
    
    <apex:pageBlock title="Oppertunity List" id="opprty">
        <apex:pageBlockTable value="{!oppList}" var="opp">
            <apex:column value="{!opp.name}"/>
            <apex:column value="{!opp.StageName}"/>
        </apex:pageBlockTable>
    
    </apex:pageBlock>
   
 </apex:form>
 
</apex:page>



Please suggest what's i am doing wrong.

Thanks
mukesh

Amit Singh 1Amit Singh 1
May I suggest to replace String searchItem = '*'+selectedValue+'*'; Line with String searchItem = selectedValue; Line and make sure you have account with the Selected Name.

Thanks!
This was selected as the best answer
Sanjay Rathore5Sanjay Rathore5

Your code is correct but you mistake in
String searchItem = '*'+selectedValue+'*';
if you wlidcard card query then you have to udr LIke with '%'(Multi character) or '_'(Single character)

Thanks