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
imAkashGargimAkashGarg 

fetch child items in visualforce page

I have to get all the orders printed in pdf format. Each order has some related order line items. i have used <apex:repeat value="{!order}" var="ord"> for the order loop. Now from the loop how can i get the related child Order_line record's values. I cannot use related list as it will give all values of the records, but i want some of its values.

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
David WrightDavid Wright

The way you would do this depends on how your database is implemented. It sounds like you have a Master Object, "Order", and a child object, "Order Line Item." I'm going to answer your question based on that assumption.

 

In your controller, you'll need to query the order line items. You can then reference the list of child objects directly in Visualforce using the API child relationship name.

 

For instance, if you wanted to query all contacts belonging to accounts, you would do the following query:

 

[SELECT Id, (SELECT Id, Name FROM Contacts) FROM Account]

 

Notice two things here: The nested SELECT statement, and the relationship name for the Contact related list, "Contacts."

 

Your query will probably look something like this:

[SELECT Id, (SELECT Id, Name FROM Order_Line_Items__r) FROM Order]

 

You can then iterate throught the line items of the order using {!order.Order_Line_Items__r} in conjunction with an apex:repeat or another iterative element.

 

(I've never tried iterating through with a repeat and then using a nested repeat to iterate over a sub-list, but I have no reason to think that won't work)

 

To find out what the relationship name is, go to the child object (Order Line Item in your case) and click on the Master Detail field for "Order". The Child Relationship Name is listed on that page under the Master-Detail Options section (make sure you append '__r' to it in your code.

 

If my assumptions about how your objects are set up is wrong, please clarify how you are set up so I can answer your question better.

All Answers

David WrightDavid Wright

The way you would do this depends on how your database is implemented. It sounds like you have a Master Object, "Order", and a child object, "Order Line Item." I'm going to answer your question based on that assumption.

 

In your controller, you'll need to query the order line items. You can then reference the list of child objects directly in Visualforce using the API child relationship name.

 

For instance, if you wanted to query all contacts belonging to accounts, you would do the following query:

 

[SELECT Id, (SELECT Id, Name FROM Contacts) FROM Account]

 

Notice two things here: The nested SELECT statement, and the relationship name for the Contact related list, "Contacts."

 

Your query will probably look something like this:

[SELECT Id, (SELECT Id, Name FROM Order_Line_Items__r) FROM Order]

 

You can then iterate throught the line items of the order using {!order.Order_Line_Items__r} in conjunction with an apex:repeat or another iterative element.

 

(I've never tried iterating through with a repeat and then using a nested repeat to iterate over a sub-list, but I have no reason to think that won't work)

 

To find out what the relationship name is, go to the child object (Order Line Item in your case) and click on the Master Detail field for "Order". The Child Relationship Name is listed on that page under the Master-Detail Options section (make sure you append '__r' to it in your code.

 

If my assumptions about how your objects are set up is wrong, please clarify how you are set up so I can answer your question better.

This was selected as the best answer
imAkashGargimAkashGarg

Thanks,

It worked. I was missing that nested SOQL query.

ajnixxajnixx

Hi akashgarg,

 

I am new to visualforce development. And i have similar requirements with yours. Please may i know how did you successfully implement the solution for this?  Thanks...

Kelly WalkerKelly Walker
David Wright,

Would I be able to use this same logic to print fields from multiple Quotes from the parent Opportunity object?

If so, can you please help me figure out the code to use? 

Thank you,

Kelly