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
nileshjnileshj 

How to iterate over nested queries?

I have a query

[SELECT Id, Type,
                                            CreatedById, CreatedBy.Name,
                                            CreatedDate, LastModifiedDate,
                                            ParentId, Parent.Name, Parent.Type,
                                            RelatedRecordId, CommentCount,LikeCount,
                                            Body, Title, LinkUrl, ContentData, ContentFileName,
                                                (SELECT Id, FieldName, OldValue, NewValue
                                                 FROM FeedTrackedChanges ORDER BY Id DESC),
                                                (SELECT Id, CommentBody, CreatedDate,
                                                 CreatedBy.Name, InsertedBy.SmallPhotoUrl
                                                 FROM FeedComments fc ORDER BY CreatedDate DESC LIMIT 2),
                                                (SELECT CreatedBy.Name
                                                 FROM FeedLikes)
                                            FROM NewsFeed  ORDER BY CreatedDate DESC LIMIT 4];



I want to use     InsertedBy.SmallPhotoUrl       field from inner query in my visualforce page.

How this can be achived?

 

Best Answer chosen by Admin (Salesforce Developers) 
MarrisMarris

Hi Nilesh

 

Below I provided you a sample for standard object related subquery and how to fetch the subquery field in Visual force page.This might be helpful for you

 

public class acc{
    public List<Account> getAccount(){
      list<Account> liParents = [select Id, Name,(select Id, Name from Contacts) from Account];
      return liParents;
    }
}

 VF page

 

<apex:page controller="acc">
  <apex:repeat var="p" value="{!Account}">
    <apex:outputText value="{!p.Name}: "/>
        <apex:repeat var="c" value="{!p.Contacts}">
        <li><apex:outputText value="{!c.Name}"/></li>
        </apex:repeat>
   </apex:repeat>
</apex:page>

 

Like the above sample,you can fetch your subquery field  'InsertedBy.SmallPhotoUrl' in your VF page.

 

Thanks

Marris

All Answers

JitendraJitendra

Please try below code:

 

 List<NewsFeed> lstNewsFeed =  [SELECT Id, Type,
								CreatedById, CreatedBy.Name,
								CreatedDate, LastModifiedDate,
								ParentId, Parent.Name, Parent.Type,
								RelatedRecordId, CommentCount,LikeCount,
								Body, Title, LinkUrl, ContentData, ContentFileName,
									(SELECT Id, FieldName, OldValue, NewValue
									 FROM FeedTrackedChanges ORDER BY Id DESC),
									(SELECT Id, CommentBody, CreatedDate,
									 CreatedBy.Name, InsertedBy.SmallPhotoUrl
									 FROM FeedComments fc ORDER BY CreatedDate DESC LIMIT 2),
									(SELECT CreatedBy.Name
									 FROM FeedLikes)
								FROM NewsFeed  ORDER BY CreatedDate DESC LIMIT 4];
								
for(NewsFeed nw : lstNewsFeed)
{
	FeedComments[] fdCmnt = nw.FeedComments;
	if(fdCmnt != null && fdCmnt.size() > 0)
	{
		for(FeedComments fc : fdCmnt)
		{
			//access using fc.InsertedBy.SmallPhotoUrl
		}
	}
}
											
											

 

MarrisMarris

Hi Nilesh

 

Below I provided you a sample for standard object related subquery and how to fetch the subquery field in Visual force page.This might be helpful for you

 

public class acc{
    public List<Account> getAccount(){
      list<Account> liParents = [select Id, Name,(select Id, Name from Contacts) from Account];
      return liParents;
    }
}

 VF page

 

<apex:page controller="acc">
  <apex:repeat var="p" value="{!Account}">
    <apex:outputText value="{!p.Name}: "/>
        <apex:repeat var="c" value="{!p.Contacts}">
        <li><apex:outputText value="{!c.Name}"/></li>
        </apex:repeat>
   </apex:repeat>
</apex:page>

 

Like the above sample,you can fetch your subquery field  'InsertedBy.SmallPhotoUrl' in your VF page.

 

Thanks

Marris

This was selected as the best answer
PremanathPremanath

Good Work Marries

nileshjnileshj

Thanks Marris :)

 

MarrisMarris

     

     Thanks Premanath.

     

      You are welcome nileshj:-)