0

using selectoption my all account name fetched if i select anyone account name in that dropdownlist show releated contact(child record) on vfpage

error showing that list has no rows

----------------controller---------------

public class AccountController {

  public Contact con { get; set; }
  public String selectedaccountId { get; set; }
  public List<SelectOption> getListOfAccounts() {
       List<Account> AccountList = [select id,Name from Account] ;
       List<SelectOption> AccountOptionList = new List<SelectOption>();
       AccountOptionList .add(new SelectOption( ' ' ,'---Select---'));

       for(Account acc : AccountList )
       {
                  AccountOptionList.add(new SelectOption(acc.id , acc.Name));
       }
      return AccountOptionList ;
}

public PageReference loadDetails(){
    con = [Select LastName, FirstName From Contact Where Id = :selectedaccountId ];
    return null;
 }
}

-----------------------vfpage------------------

<apex:page controller="AccountController">
   <apex:form >
      <apex:pageBlock >
           <apex:pageBlockSection title="Select the Account" >
              <apex:OutputPanel >
                <apex:selectList value="{!selectedaccountId}" size="1" multiselect="false"  >
                  <apex:selectOptions value="{!ListOfAccounts}" />
                  <apex:actionSupport event="onchange" 
                    action="{!loadDetails}" 
                    rerender="contactDetails"/>

               </apex:selectList>
          </apex:OutputPanel>

      </apex:pageBlockSection>
      <apex:pageBlockSection title="Details" columns="1" id="contactDetails">
             <apex:outputField value="{!con.firstname}"/>
                <apex:outputField value="{!con.lastname}"/>
               </apex:pageBlockSection>
  </apex:pageBlock>
</apex:form>

1
  • 2
    You're displaying accounts in your list and you're trying to display the details of a contact. So there is a problem with the selectedaccountId. It matches an account id but you're requesting a contact id. Feb 28 '17 at 9:54
2

There may have multiple contact related to selected Account. You need to iterate contact list to display all the related contacts.There is an error in your query, it should be AccountId insted of id.

<apex:page controller="AccountControllertest">
<apex:form id="formid">
    <apex:pageBlock>
        <apex:pageBlockSection title="Select the Account">
            <apex:OutputPanel>
                <apex:selectList value="{!selectedaccountId}" size="1" multiselect="false">
                    <apex:selectOptions value="{!ListOfAccounts}" />
                    <apex:actionSupport event="onchange" action="{!loadDetails}" rerender="formid" />
                </apex:selectList>
            </apex:OutputPanel>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Details" columns="1" id="contactDetails" rendered="{!(lstContact.size > 0)}">
            <apex:pageBlockTable value="{!lstContact}" var="con">
                <apex:column value="{!con.LastName}" />
                <apex:column value="{!con.FirstName}" />
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

Controller for the page -

public class AccountControllertest {

  public List<Contact> lstContact { get; set; }
  public String selectedaccountId { get; set; }
public AccountControllertest(){
    lstContact = new List<Contact>();
}
  public List<SelectOption> getListOfAccounts() {
       List<Account> AccountList = [select id,Name from Account] ;
       List<SelectOption> AccountOptionList = new List<SelectOption>();
       AccountOptionList .add(new SelectOption( ' ' ,'---Select---'));

       for(Account acc : AccountList )
       {
                  AccountOptionList.add(new SelectOption(acc.id , acc.Name));
       }
      return AccountOptionList ;
}

public PageReference loadDetails(){
    lstContact = [Select LastName, FirstName From Contact Where AccountId = :selectedaccountId ];
    return null;
 }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.