function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Xeon ArianXeon Arian 

Unable to filter using ContentDocument id in query in trigger query not retrieving any results

I have a requirement to make to make a file private and be available to only the user whose role name consists the name of the file for a specific custom object. For this I am trying to retrieve from Content Document Link with the custom object name LinkedEntity.Type and ContentDocumentid as filters, when I hard code the ContentDocumentid it is working fine but when I try to dynamically provide the ContentDocumentId then the query is not returning any result. I am adding a snippet of my code. Please Help!!. Thanks
List<Id> listOfConDocuId = new List<Id>();

    for(ContentVersion cv:{
        if((!cv.Title.contains('product proposal')) || (!cv.Title.contains('final')) || (!cv.Title.contains('packet')))
    Map<Id, Project__c> mapOfProjectId = new Map<Id, Project__c>([SELECT Id FROM Project__c]);
    Set<Id> setOfProjectId = mapOfProjectId.keySet();
    List<ContentDocumentLink> LinkedProject = [SELECT ContentDocumentId, LinkedEntityId, ContentDocument.Title FROM ContentDocumentLink where LinkedEntityId in :setOfProjectId and LinkedEntity.Type='Project__c' and ContentDocumentId IN :listOfConDocuId];

NagendraNagendra (Salesforce Developers) 
Hi Arian,

What kind of trigger is it, before insert? It might be the case that at that point there's no "parent" (ContentDocument). And ContentDocumentLinks come even later in the process (first ...version and ...document has to be created, then you use to connect ...document to your custom object). Run System.debug(listOfConDocuId); see if they come out as nulls or something else. If they come out null - you're starting in the wrong place and making a trigger on CDL might be a better idea. 

Also, please check below link. Please let us know if this helps.

Deepak Pandey 13Deepak Pandey 13
Hello Xeon, 
Please check this filter LinkedEntity.Type='Project__c'. Because  LinkedEntity is hold only object data.
You can filter like
SELECT ContentDocumentId, LinkedEntityId,ContentDocument.Title, ShareType, ContentDocument.OwnerId  FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM Account)