I am using SF Mobile SDK to do a SmartSync. Because of that, i need to make sure that the results come down in one query.

I want to select data from Task where one of the following is true:

  • OwnerId is a particular user id (Update this means, regardless of presence of What on the Task)
  • What.Type = 'Account' and Account.OwnerId is a particular user id
  • What.Type = 'Account' and Account.Owner.ManagerId is a particular user id

I can get the bottom two like so:

SELECT Id, Status, Subject FROM Task 
(What.Type = 'Account' 
   AND WhatId IN 
     (SELECT Id FROM Account WHERE Owner.Manager.Id = '<USERID>' 
        OR OwnerId = '<USERID>'))

However, trying to add an OR OwnerId = '<USERID>' to this query results in the dreaded semi-joins error:

Semi join sub-selects are only allowed at the top level WHERE expressions and not in nested WHERE expressions.

Is there any way to make this work in a single SOQL?

Again, repeat, not APEX, and really need to avoid two queries.

  • I am not familiar with the Mobile SDK - is it an option to run SOQL that is slightly too broad and filter on the client side? You can do it with an AccountId filter, but the semantics are slightly different.
    – David Reed
    Apr 10, 2019 at 5:30
  • @DavidReed I wish. It works like so: you give it a soql query (or, for simpler cases, a set of fields and sObject name) and it hides away all saving/manipulating from you. it is convenient in that it does incremental fetches but is somewhat limiting for cases like this.
    – zaitsman
    Apr 10, 2019 at 5:48

1 Answer 1


You can actually just do the following:

WHERE What.Type = 'Account' AND
(OwnerId = '<UserId>' OR Account.OwnerId = '<UserId>' OR Account.Owner.ManagerId = '<UserId>')

No sub-query is necessary here.

  • sorry, this will give me different data. If you see the conditions in the question, i need OwnerId is a particular user id that means irrespective of whether there is a What on the record. Whereas what you posted will only return tasks where what is 'Account'
    – zaitsman
    Apr 10, 2019 at 5:47
  • Actually, i was able to edit your suggestion a little big so it is now WHERE OwnerId = '<UserId>' OR (What.Type = 'Account' AND (Account.OwnerId = '<UserId>' OR Account.Owner.ManagerId = '<UserId>')) and that does seem to work!
    – zaitsman
    Apr 10, 2019 at 5:51

You must log in to answer this question.

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