I am trying to get the uncompleted tasks of every lead in a Lead View, when I try to get all the tasks it works fine, but when I add in the "where Status != 'Completed'" I get the following exception:

core.apexpages.exceptions.ApexPagesHandledException: Object type not accessible. Please check permissions and make sure the object is not in development mode: Status != 'Completed') from Lead AND ((IsConverted = FALSE)) AND ^ ERROR at Row:1:Column:87 unexpected token: AND

the code I am using to do this is:

Database.QueryLocator ql = Database.getQueryLocator('select Id, (select Id, ActivityDate from Tasks') from Lead limit 1');
//doesnt work
Database.QueryLocator ql = Database.getQueryLocator('select Id, (select Id, ActivityDate from Tasks where Status != \'Completed\') from Lead limit 1');
ApexPages.StandardSetController setController = new ApexPages.StandardSetController(ql);


List<Lead> filtered = setController.getRecords();

What does the error mean? Is there something I can do to configure my salesforce account so this will work? or do I just need to change the query?

If it matters I am currently running this code from the developer console

  • Have you tried without escape characters? i.e. where Status != Completed
    – highfive
    Oct 21, 2014 at 4:22
  • that gives me the error "System.QueryException: expecting a colon, found 'Completed'"
    – user8246
    Oct 21, 2014 at 5:43
  • Looks like your code and error doesn't match. As per the error you are using AND keyword incorrect. But can't see it in your code. Can you verify this?
    – highfive
    Oct 21, 2014 at 6:18
  • I believe that is related to the Lead View that I am filtering by using the line "setController.setFilterID('00Bi0000006BgwZ');" I guess the Lead View gets applied to my query in a way that causes this issue, but I dont know if/how I can get both the subquery I want while still filtering by that Lead View. It seems like the application of the Lead View is causing the StandardSetController to generate an invalid query, is there maybe a different way of either filtering by a Lead View, or reformulating my subquery that could possibly circumvent this issue?
    – user8246
    Oct 21, 2014 at 7:45

1 Answer 1


Your query you posted as "the code i am using" does not match the error presented.

The error specifically stated the query used has this

Status != 'Completed') from Lead AND ((IsConverted = FALSE)) AND

in it.

It should be

'select Id, (select Id, ActivityDate from Tasks where Status != \'Completed\') from Lead Where IsConverted = false limit 1'

Can you confirm that the exact query you posted does indeed work as is does in execute anonymous.

According to the error, you have AND after the from Object without a preceding Where.

  • I believe that is being added by the Lead View I am filtering by
    – user8246
    Oct 21, 2014 at 5:38
  • @user8246 - Not sure what you mean but stop it from doing it
    – Eric
    Oct 21, 2014 at 5:40
  • Part of what I am trying to do is get all the leads in a Lead View which is why I have the line "setController.setFilterID('00Bi0000006BgwZ');" that is an important part of the query I am trying to make, if I stop filtering by the Lead View, it may stop the error but I would no longer be getting the leads I want so that doesnt solve anything.
    – user8246
    Oct 21, 2014 at 5:47
  • A solution would be to rewrite the Lead View as a Where clause for your SOQL query. It would make the code less dependent on the View (should anyone choose to accidentally change or even delete it). Your code would also be more readable. Also: using IDs like that in code is not considered good practice. Apr 21, 2015 at 13:22

You must log in to answer this question.

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