The following code returns the error:

List<ContentDocumentLink> docLinks = [SELECT ContentDocumentId FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM Case ) and LinkedEntity.Type='Case'];

Set<String> docIds = new Set<String>();

for(ContentDocumentLink cdl : docLinks){

List<ContentDocument> docsToDelete = [SELECT Id FROM ContentDocument WHERE Id IN docIds];

And this is the exact error:

enter image description here

I don't see why this should be happening and I suspect it may be a bug. Does anyone know what causes this?

  • What happens when you comment out the last two lines? Jun 30, 2021 at 17:16
  • 3
    In your query on line 9, are you really missing the : in the Id IN docIds, or was that just a small typo? Yeah, it doesn't really have to do with what the error is reporting, but these unexpected token errors can be caused by darn near anything in my experience.
    – Derek F
    Jun 30, 2021 at 17:19
  • Excellent catch @DerekF Jun 30, 2021 at 17:19
  • @DerekF that was totally it, can't believe I missed it. Please post as answer so I can give you credit.
    – Adam Carr
    Jun 30, 2021 at 17:34

1 Answer 1


Running this through my own execute anonymous window, I was able to reproduce the issue.

Turns out that the problem is indeed the missing colon : in your query.

When using a variable in a query (typically called a bind, or binding, see relevant documentation), we need to use : to let Salesforce know that it is a variable.

So the correct query would be List<ContentDocument> docsToDelete = [SELECT Id FROM ContentDocument WHERE Id IN :docIds];

As for why this generates that particular error, I'm not sure. Best guess would be that some combination of how anonymous apex sends code to Salesforce, and the lexer Salesforce is using causes some tokenization issues earlier in the line.

  • Got a similar error when I forgot to add a + at the end of one line of a multi-line concatenated string. Salesforce could definitely improve on their error reporting here...
    – Frans
    Aug 3, 2021 at 19:03

You must log in to answer this question.

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