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
BPOORBPOOR 

Insert a record in custom object when attaching files in Salesforce

I am working on as assignment where I need to insert a record in a custom object (FileAttachment__c) when a file is uploaded under a Contact. When the file is uploaded, the custom object record needs to be populated with the following values.
FileAttachmentId ==> Id of the ContentDocument
FileAttachmentName ==> Name of the ContentDocument
Contact__c = Contact on which the file was uploaded.

I created a before trigger on the ContentVersion object to achieve this. However, I am having problems when querying the ContentDocumentLink object based on the ContentDocumentId on the ContentVersion. My sample code where I am querying ContentDocumentLink is below.
 
List<FileAttachment__c> lstFAT = new List<FileAttachment__c>();
    Set<Id> contentDocumentIdSet = new Set<Id>();
    Map<Id,Id> mapContentDocumentIdToContactId = new Map<Id,Id>();
    for (ContentVersion cv : lstCV) {
        contentDocumentIdSet.add(cv.ContentDocumentId);
    }
    //List<Id> contentDocumentIdList = new List<Id>(contentDocumentIdSet);
    List<ContentDocumentLink> lstCDL = new List<ContentDocumentLink>();
    lstCDL = [SELECT Id, ContentDocumentId, LinkedEntityId from ContentDocumentLink WHERE ContentDocumentId in :contentDocumentIdSet];
    if (lstCDL != null && !lstCDL.isEmpty()) {
        for (ContentDocumentLink cdl : lstCDL) {
            if (cdl != null && cdl.LinkedEntityId.getSObjectType().getDescribe().getName() == 'Contact') {
                mapContentDocumentIdToContactId.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
            }
        }
    }
However, when I test the code, I am getting the error message "ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.:".
Then I changed the code to replace ContentDocumentIDSet with ContentDocumentIDList (Based on previous searches from StackExchange, and Populated the List from the Set) and tried to test it. However, I got the same error.
Then I changed the code to build a String with the list of Ids from the ContentDocumentIDSet and used in the WHERE clause of ContentDocumentLink query. However, this time I am getting the error message "IN operator must be used with an iterable expression ".
Can someone help? In the worst case scenario, I am thinking of handling this in the ContactTrigger. However, the ContactTrigger in our Org is already big and I prefer to avoid adding these changes in the ContactTrigger.