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
Vishnu7700Vishnu7700 

Accounts with associated contacts

Hi with useing <apex:repeat> trying to display accounts with associated contacts on VF page,but in controller i'm writing an relationship query as mentioned in controller.

Error:Compile Error: unexpected token: 'Select' at line 7

VF:

<apex:page controller="AsFive" sidebar="false" tabStyle="Account">
<apex:form >
<apex:message />
<apex:sectionHeader title="AssignmentFive"/>
<apex:pageBlock title="Accounts with assoicated Contacts"></apex:pageBlock>
<apex:repeat value="{!list}" var="acc">
<apex:pageBlockSection title="{!acc.name}">
<apex:pageBlockTable value="{!acc.contact}" var="con">
<apex:column value="{!con.Firstname}"/>
<apex:column value="{!con.Lastname}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:repeat>
</apex:form>
</apex:page>

 

Controller class

public class AsFive {
//Constructo method called
public AsFive(){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'this page was created with only one query'));
}
public List<Account> getList() {
return [Select Name,Id (Select Firstname, Lastname FROM contacts) FROM Account order by Name];
}
}

asish1989asish1989

  retrun [Select Name,Id, (Select Firstname, Lastname FROM contacts) FROM Account order by Name];

<apex:pageBlockTable value="{!acc.contacts}" var="con">

 

puneet28puneet28

Hi Vishnu7700,

 You are missing a  ',' 

 

SOQL should be :

 

Select Name,Id, (Select Firstname, Lastname FROM contacts) FROM Account order by Name

 

 

Change your Visualforce Page to

 

<apex:page controller="AsFive" sidebar="false" tabStyle="Account">
<apex:form >
<apex:message />
<apex:sectionHeader title="AssignmentFive"/>
<apex:pageBlock title="Accounts with assoicated Contacts">
<apex:repeat value="{!list}" var="acc">
<apex:pageBlockSection title="{!acc.name}">
<apex:pageBlockTable value="{!acc.contacts}" var="con">
<apex:column value="{!con.Firstname}"/>
<apex:column value="{!con.Lastname}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:repeat>
</apex:pageBlock>
</apex:form>
</apex:page>

 

and Controller to:

 

public class AsFive {
//Constructo method called
public AsFive(){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'this page was created with only one query'));
}
public List<Account> getList() {
return [Select Name,Id, (Select Firstname, Lastname FROM contacts) FROM Account order by Name];
}
}

 

Also one more thing you are quering all your Accounts. You should probably limit the number of accounts that will be retrieved by SOQL.

 

For limiting use this SOQL :

 

Select Name,Id, (Select Firstname, Lastname FROM contacts) FROM Account order by Name LIMIT 10

 

 

Vinita_SFDCVinita_SFDC

Hello,

 

Try like this:

 

<apex:page controller="AsFive" recordSetvar="accounts" sidebar="false" tabStyle="Account">
<apex:form >
<apex:message />
<apex:sectionHeader title="AssignmentFive"/>
<apex:pageBlock title="Accounts with assoicated Contacts">
<apex:repeat value="{!accounts}" var="acc">
<apex:pageBlockSection title="{!acc.name}">
<apex:pageBlockTable value="{!acc.contact}" var="con">
<apex:column value="{!con.Firstname}"/>
<apex:column value="{!con.Lastname}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:repeat>
</apex:form>

</apex:pageBlock>
</apex:page>

souvik9086souvik9086

Change the apex code soql query

 

Controller class

public class AsFive {
//Constructo method called
public AsFive(){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'this page was created with only one query'));
}
public List<Account> getList() {
return [Select Name,Id, (Select Firstname, Lastname FROM contacts) FROM Account order by Name];
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Vishnu7700Vishnu7700

Hey got solution!!!!

public class AssignmentFive {

//List Varaibles
Public List<account> accList{get;set;}

//Construct method
public AssignmentFive ()
{
accList = [SELECT Name, (SELECT FirstName, LastName, Phone, Email FROM Contacts) FROM Account];
}
}