Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I have a rather complicate query that I am trying to do all in one step which may or not be possible

To try and explain I have custom objects called "paths" and paths are specialized content available for specific types of users tied to specific "actions" which are our main content. Theres two other custom objects. Path Groups (which is just a master detail with paths) and then (Path Group Permissions) which is what grants permission to accounts to specific paths..

An example might help

So we have a path group called Homeownership, this path contains many little bits of info (called paths)... and then certain portal users can see this information so they have a path group permission on their account record

So the question is, my visualforce page is looping through all the actions and I want it to also display any paths available to the user.

This bit of code is working now:

string userId = UserInfo.getUserId();
User u = [select ContactId, Email, FirstName, LastName, Title 
          from User where Id=:userId];

Contact c = [select id, AccountId, Contact.Account.Name, Contact.Account.Id
             from Contact where id = :u.ContactId];

List<Path_Group_Permission__c> pgp = [select 
    from Path_Group_Permission__c
    where Path_Group_Permission__c.Account__r.Id = :c.Account.Id];

Actions =  [Select Name, body__c, Action_Type__c, Caution__c,  URL_Title__c, 
    (select Path__c.Id, Path__c.Name, Path__c.Body__c from Action__c.Paths__r
        where Path__c.Path_Group__c = :pgp[0]
    from Action__c order by Order__c ];

the problem is its only checking the first "PGP" or path group permission. I feel like I should be able to use "IN" to check all of them but I am not having any luck

I tried

  • Path__c.Path_Group__c IN :pgp
  • Path__c.Path_Group__c IN
  • and adding the pgp query into the actions query like Path__c.Path_Group__c in **subquery**

no luck... any ideas?

share|improve this question
up vote 2 down vote accepted

Iterate over the pgp list and accumulate all the path group ids in a List and then use this as the predicate for an IN condition

List<Id> pgIds = new List<Id>{};
For (Path_Group_Permission__c pgperm : pgp)

And then use the collection in the Where

where Path_c.Path_Group__c IN :pgIds

As an aside you're not filtering on Actions, so could there be Action records which don't have associated Path records?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.