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?

    Can you log the value of mapOfContentVersion.keySet() - what does it return?

  • harish

    harish Correct answer

    5 years ago

    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

    It does but why? map.keySet() and a set , aren't they both same thing?

    In a unit test, this works only if you execute the query one time but not two times before StartTest. The second time I get the same error. After StopTest I can query again filtering by the new set without issues. Does anyone have any idea why this is happening?

    Not sure how this one works, didn't work for me unfortunately. Exec. Anonymous Code: `Map mapCV = new Map(); Set setId = mapCV.keyset(); List listCDL = [SELECT LinkedEntityId, ContentDocumentId, Visibility FROM ContentDocumentLink WHERE ContentDocumentId IN: setId];`

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM