ContentDocumentLink filter error on ContentDocumentID using IN clause

I have to get all the “linked” records from a Document (ContentDocument) and then filter only the Events from them using the following apex.

// Query ContentDocumentLink object for the 'Event' link and apply 'WHERE' filter of the above map's contentdocument id.
Map<id, Event> mapEvent = new Map<id, Event>();
Set<Id> EventIds = new Set<Id>();
for(ContentDocumentLink cdlForEvent: [SELECT ContentDocumentID, LinkedEntity.type, LinkedEntityId 
                                    FROM ContentDocumentLink 
                                    WHERE ContentDocumentID IN: mapOfContentVersion.keySet()])
{
    if(cdlForEvent.LinkedEntity.Type == 'Event')
    {
        EventIds.add(cdlForEvent.LinkedEntityId);
    }
}

I’m using ContentDocumentId filter, from a contentversion map but it is giving me following 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.

Interestingly, if I provided hard-coded values using IN clause then it accepts it. But I have to make it run in the apex and it has to be dynamic. Following soql works

for(ContentDocumentLink cdlForEvent: 
                [SELECT ContentDocumentID, LinkedEntity.type, LinkedEntityId 
                FROM ContentDocumentLink 
                WHERE ContentDocumentID IN ('06923000000CqHNaxy', '06923000000CqHNAA0')])

I dont understand what is missing. How should I get all the related linked entity records and then filter Events from them?

Answer

the solution is to add your keySet() from Map to a new Set() and use that in the query filter using IN:

Ex:

Set<Id> newSet = mapOfContentVersion.keySet();


[SELECT ContentDocumentID, LinkedEntity.type, LinkedEntityId 
                                    FROM ContentDocumentLink 
                                    WHERE ContentDocumentID IN: newSet ]

**This worked for me today

Attribution
Source : Link , Question Author : Mahmood , Answer Author : Ratan Paul

Leave a Comment