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
Sammy7Sammy7 

getAccount () method to return multiple queries or set?

Hi,
 So Im trying to display a custom visualforce page to list three separate accounts (by id and name) and their related contacts list by using a custom controller below.
But I dont know how to get accounts for Acme2 and Acme3 in the class using the same method. Any help is appreciated. Thanks.
public class MyController {

    public String getName() {
        return 'MyController';
    }

    public Account getAccount() {
        return [SELECT id 
                from Account 
                 where Name = 'Acme'];                       
    } 
}

and this is my visualpage:
<apex:page controller="MyController" tabStyle="Account">
     <apex:pageBlock title="Acme"> 
        <apex:relatedList subject="{!account}" list="Contacts"/>
    </apex:pageBlock>
    <apex:pageBlock title="Acme2"> 
        <apex:relatedList subject="{!account}" list="Contacts"/>
    </apex:pageBlock>
     <apex:pageBlock title="Acme3"> 
        <apex:relatedList subject="{!account}" list="Contacts"/>
    </apex:pageBlock>
</apex:page>

 
Best Answer chosen by Sammy7
Prateek Singh SengarPrateek Singh Sengar
Hi Sammy,
You need to query all three accounts and add them to a list and then use apex:repeat to get the related list. 
Please see below a sample code
 
<apex:page controller="myController" tabstyle="Account">
  <apex:pageBlock title="three views">
    <!-- Iterate through list and display related list -->
      <apex:repeat value="{!accountlist}" var="acc">
          <apex:pageBlock >
              <apex:relatedList subject="{!acc}" list="Contacts"/>
          </apex:pageBlock>
      </apex:repeat>
  </apex:pageBlock>
</apex:page>

Apex Class

public class myController {

   // query all accounts and store in a list
    public List<Account> getAccountlist()
    {
        List<Account> accList = [Select Id From Account limit 3];
        return accList;
    }
}

 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Sammy,
You need to query all three accounts and add them to a list and then use apex:repeat to get the related list. 
Please see below a sample code
 
<apex:page controller="myController" tabstyle="Account">
  <apex:pageBlock title="three views">
    <!-- Iterate through list and display related list -->
      <apex:repeat value="{!accountlist}" var="acc">
          <apex:pageBlock >
              <apex:relatedList subject="{!acc}" list="Contacts"/>
          </apex:pageBlock>
      </apex:repeat>
  </apex:pageBlock>
</apex:page>

Apex Class

public class myController {

   // query all accounts and store in a list
    public List<Account> getAccountlist()
    {
        List<Account> accList = [Select Id From Account limit 3];
        return accList;
    }
}

 
This was selected as the best answer
Sammy7Sammy7
Is there any way to use the .add for doing this?
List <Account> acc List; 
acc.add [SELECT id from Account where Name = 'Acme'];
acc.add [SELECT id from Account where Name = 'Acme2'];
acc.add [SELECT id from Account where Name = 'Acme3'];

Will this work?
Prateek Singh SengarPrateek Singh Sengar
Hi Sammy,
Yes that would work but thats a bad practice as you are using 3 SOQL instead of 1. A better solution would be

List<Account> accList;

accList = [Select Id from Account where Name = 'Acme' OR Name = 'Acme2' OR Name = 'Acme3'];

Please mark the thread as solved if this answers your query.
Sammy7Sammy7
Perfect ! Thanks.