With a Visualforce page for my Community, I am trying to limit the records returned from a SOQL based on criteria of child records.

I have a custom object Sale (parent) and custom object Sale Related Contact (child via a master/detail relationship). Sale Related Contact has a lookup to the standard Contact object and the standard User object also has a lookup to Contact.

What I want to do is return only Sale records where the current user is a contact in a related Sale Related Contact record and Type = Realtor.

My controller will compile (applicable code below) but whenever a user logs in that meets this criteria (ie a sale record should be returned) I get the generic "Error: Error occurred while loading a Visualforce page. Please email us if you need to get in touch." screen.

When logging in as a user who does meet this criteria (ie a sale record should be returned), I get no error.

Thoughts on what I'm doing wrong?

private User thisUser;
private List<Sale_Related_Contact__c>  srcRealtors;
private List<Id>  saleRealtor;
private List<Sale__c>  saleRec;

User thisUser = [select Id, AccountId, ContactId from User where Id = :UserInfo.getUserId() ];
srcRealtors = [select Id, Contact__c,Sale__c, Type__c from Sale_Related_Contact__c where Contact__c = :thisUser.ContactId AND Type__c = 'Primary Realtor' ]; 
for(Sale_Related_Contact__c src : srcRealtors){
saleRec = [select Id from Sale__c where Id in :saleRealtor);
  • 1
    Other than the fact that you don't appear to be initializing saleRealtor, which I assume must be elsewhere, because the saleRec query isn't crashing, there's no inherent problems with this code. You'll need to do some debugging using Setup > Monitoring > Debug Logs, then get back with us.
    – sfdcfox
    Jun 19, 2015 at 19:16
  • I'm surprised that your code would compile when you have a query statement starting with a bracket and ending with a parenthesis.
    – martin
    Jun 19, 2015 at 19:36
  • Thanks for the pointer to the log file, that helped to resolve this. Turns out I wasn't querying for all the fields that later logic was using.
    – John_Henry
    Jun 19, 2015 at 19:54

1 Answer 1


I believe that this should work for you:

private User thisUser;
private List<Sale__c>  saleRec;
thisUser = [SELECT Id, ContactId FROM User WHERE Id = :UserInfo.getUserId()];
saleRec = [SELECT Id FROM Sale__c WHERE Id IN (SELECT Sale__c FROM Sale_Related_Contact__c WHERE Contact__c = :thisUser.ContactId AND Type__c = 'Primary Realtor')];
  • In the question its mentioned that Contact is a lookup to Contact object. So Contact__c =: UserInfo.getUserId() wouldn't work. Jun 27, 2015 at 18:10
  • True, I'll have to take another look at this one.
    – martin
    Jun 27, 2015 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .