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
Michael Johnson 29Michael Johnson 29 

Nested Queries Not Returning Using a Referenced Apex Class

I am having an issue where nested query results are not returning when being passed through a different class. 

Here is my scenario.

Master Object: Project__c
Child Object: ProjectRole__c

When using a subquery in the same class, the related subqueried objects show up just fine.

list<Project__c> pl = [Select Id, Name, (Id, Name from ProjectRoles__r) from Project__c];
system.debug(pl.get(0).ProjectRole__r);

Running this query in a class or in the developer console returns the ProjectRoles__r subquery just fine.

DEBUG|(ProjectRole__c:{Project__c=a0lg0000002tbnuAAA, Id=a0mg00000048QUTAA2, Name=PRR-0000117848}, ProjectRole__c:{Project__c=a0lg0000002tbnuAAA, Id=a0mg00000048QUUAA2, Name=PRR-0000117849})

However, when you reference a class to return this query, the subqueries are completely lost. 

public without sharing class QueryController {

	 public static list<Project__c> getProjects(){
 		return [Select Id, Name, (Id, Name from ProjectRoles__r) from Project__c];
 	}
}
list<Project__c> pl = QueryController.getProjects();
system.debug(pl.get(0).ProjectRole__r);
While the Project__c record is returned, none of the subqueried records are returned.
DEBUG|()

Anyone have any ideas?
Best Answer chosen by Michael Johnson 29
Naval Sharma4Naval Sharma4
Hi Michael,

The class in which you are referencing this method should be declared without sharing. 

Regards,
Naval

All Answers

Naval Sharma4Naval Sharma4
Hi Michael,

The class in which you are referencing this method should be declared without sharing. 

Regards,
Naval
This was selected as the best answer
Michael Johnson 29Michael Johnson 29
Thanks Naval, that worked.