+ Start a Discussion
ericmonteericmonte 

Nested Query and Visualforce page

Hey all I am having a hard time trying to figure out the count of the number of child records in my Parent Record and displaying it in my Visualforce.

 

Here is my current Query

 

public class myController{

public List <account> accList {get;set;}
public myController {

accList = [select id, Name, (Select id, Name from Addresses__r) from Account];


}

And my visualforce Page will look like this

<apex: repeat value="{!accList}" var="a">
ID: {!a.Id}
Name: {!a.Name}
Number of Child Records: {!getCountChild}
<apex:repeat value="{!a.Addresses__r}" var="add">
Address info id: {!add.Id}
Address Name: {!add.Name}

</apex:repeat>
</apex:repeat>

 Any help would be great on this, I've been stuck on this for weeks.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
MoUsmanMoUsman

Hi 

public class myController{

public List <account> accList {get;set;}
public myController {
	accList = [select id, Name, (Select id, Name from Addresses__r) from Account];
}

And my visualforce Page will look like this

<apex: repeat value="{!accList}" var="a">
	ID: {!a.Id}
	Name: {!a.Name}
	Number of Child Records: {!getCountChildvar
	<apex:variable var="count" value="{!0}" />
	<apex:repeat value="{!a.Addresses__r}" var="add">
		<apex:variable var="count" value="{!count+1}" />
	</apex:repeat>
{!count} </apex:repeat>

If you did not get your solution please let me know,If this was your requirment then don't forget to mark as solve to help other!  

--

Thanks

Usman

All Answers

MoUsmanMoUsman

Hi 

public class myController{

public List <account> accList {get;set;}
public myController {
	accList = [select id, Name, (Select id, Name from Addresses__r) from Account];
}

And my visualforce Page will look like this

<apex: repeat value="{!accList}" var="a">
	ID: {!a.Id}
	Name: {!a.Name}
	Number of Child Records: {!getCountChildvar
	<apex:variable var="count" value="{!0}" />
	<apex:repeat value="{!a.Addresses__r}" var="add">
		<apex:variable var="count" value="{!count+1}" />
	</apex:repeat>
{!count} </apex:repeat>

If you did not get your solution please let me know,If this was your requirment then don't forget to mark as solve to help other!  

--

Thanks

Usman

This was selected as the best answer
ericmonteericmonte

hey MoUsman thanks for reply, but I'm kind of confuse on how you are using:  {!getCountChildvar}? Are you taking this value from the controller?

MoUsmanMoUsman

Please remove your that particular line from your code.I was forget to remove that!!

 

public class myController{

public List <account> accList {get;set;}
public myController {
	accList = [select id, Name, (Select id, Name from Addresses__r) from Account];
}

And my visualforce Page will look like this

<apex: repeat value="{!accList}" var="a">
	ID: {!a.Id}
	Name: {!a.Name}
	<apex:variable var="count" value="{!0}" />
	<apex:repeat value="{!a.Addresses__r}" var="add">
		<apex:variable var="count" value="{!count+1}" />
	</apex:repeat>
       Number of Child Records: {!count}
</apex:repeat>

 

Thanks

Usman

ericmonteericmonte

that worked! i knew there was an easier way to this count rather than creating a for loop!!!

 

THANKS!! now i can sleep easy tonight :)

MoUsmanMoUsman
Cheers ericmonte!!!
Don't forget to give a Kudos :P

Thanks!