I'm facing

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

on running test class.

Here is my code snippet:

Controller code:

 public static void removeAllContentDocumentFromCaseComment(List<Id> fileIds){
    system.debug('fileIds####' + fileIds);
    if(fileIds.size() > 0){
        Set<Id> versId = new Set<Id>(fileIds);
        List<ContentVersion> links = [SELECT Id,ContentDocumentId FROM ContentVersion WHERE Id IN :versId];
        Set<Id> contentDocIds = new Set<Id> ();
        for(ContentVersion cv : links ) {
        System.Debug('contentDocIds###' + contentDocIds);   
        List<ContentDocumentLink> doclinks = [SELECT Id,ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId IN :contentDocIds];

Test class:

    ContentVersion contVersion = new ContentVersion ();
    contVersion.Title = 'testTitle';
    contVersion.PathOnClient = 'testTitle'; 
    contVersion.VersionData = EncodingUtil.base64Decode('Test Version Data');
    List<ContentVersion> cvList = new List<ContentVersion>(); 
    insert cvList;
    Set<Id> verId = new Set<Id>();
    for(ContentVersion cvr : cvList) {
    List<ContentVersion> versions = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id IN:verId];
    List<Id> versIdList = new List<Id>(verId);
    List<ContentDocument> contDoc = [SELECT Id,LatestPublishedVersionId FROM ContentDocument WHERE LatestPublishedVersionId IN : verId];

When I checked in debug logs, I get "contentDocIds####" as empty which in turn results "doclinks" empty which leads to the above mentioned error.

But I'm wondering why and how contentDocIds is coming empty when I'm able to see "fileIds####" values in logs.

1 Answer 1


After inserting contentVersion in test class, Please try querying contentdocuments. then it will give contentdocument records of the test content versions inserted in test class. Then, query the contentVersion again with limit 1 as i am seeing you are inserting one contentVersion and see if you are able to get contentDocumentId like below and try

List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
    ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];

here cv is my contentVersion i just inserted in test class.

  • I tried the above , but it still results the same. I updated my code. Can you please check it. I tried with "ContentDocument.Id" instead of "ContentDocumentId" but no luck
    – uma451
    Commented Apr 3, 2019 at 9:48
  • My bad !! It worked actually. I used "ContentDocument.Id" instead of "ContentDocumentId". Also thanks @7'7' for your valuable time.
    – uma451
    Commented Apr 4, 2019 at 7:08

You must log in to answer this question.

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