0

I'm trying to query a list of ContentDocumentLinks based on a set of RelatedEntityIDs, in this case Opportunities. The code looks like this:

List<Opportunity> oppList = [SELECT id, ownerid, Name 
                               FROM Opportunity
                               WHERE Stalled__c = FALSE
                               AND Dead__c = FALSE
                               AND StageName != 'Closed Won'
                               AND StageName != 'Closed Lost'
                               AND StageName != 'Referred'      
                               ];
//Create a map and a set for later use

        Map<Id,Opportunity> omap = new Map<Id,Opportunity>(oppList);
        Set<Id> oset = omap.keyset();
//Get all the note/opportunity ids that are linked to the relevant opportunities
        List<ContentDocumentLink> noteLinkList = [SELECT ContentDocumentId, LinkedEntityID 
                                                 FROM ContentDocumentLink
                                                 WHERE LinkedEntityID IN :oset];

The error I'm receiving is:

"System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator."

I can't figure out what I'm doing wrong. I tried a lot of different things (for example, using a set of IDs instead of a list) per this link: ContentDocumentLink filter error on ContentDocumentID using IN clause but nothing has worked and the same error results. Please help!

  • The comments to this answer suggest you should cast your set of ids to a list and use the list as the bind variable. you should try in anon apex to see if it is a version issue – cropredy Jul 17 '19 at 5:13
0

This error usually occurs when you don't apply any filter to ContentDocumentLink or when your filter variable in empty or null.

So if your oppList is empty, then oset would be empty i.e. your filter variable would become empty or null and hence you will receive the error that you are getting.

Why empty or null filter causes error

Salesforce SOQL ignores the where clause when you filter on required Lookup fields by null or empty variable. And as LinkedEntityId is a required lookup field on ContentDocumentLink, and you are trying to filter it by null or empty variable, it ignores the entire where clause which leads to no filter at all in SOQL and that leads to below error.

Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.

Links :

You can refer below links to confirm the above behavior

SOQL ignores : WHERE RequiredLookup__c = null

Idea posted for same issue

| improve this answer | |
  • Hi @James, were you able to solve this problem ? – Vijay Ganji Jul 23 '19 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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